日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转]图片处理函数(自适应缩略图datatable中添加缩略图像)

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]图片处理函数(自适应缩略图datatable中添加缩略图像) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/// <summary>/// 為DataTable指定行中的生成縮略圖/// </summary>/// <param name="dataTable">數據源</param>/// <param name="dataTableImageColumnName">要生成縮略圖的數據源的列名</param>/// <param name="appendSmallPicColumnName">新增縮略圖數據列的列名</param>/// <param name="imageFormat">源圖像格式</param>/// <param name="maxWidth">圖像自適應的最大寬度</param>/// <param name="maxHeight">最像自適應的最大高度</param>public static void GetSmallPic(DataTable dataTable, string dataTableImageColumnName, string appendSmallPicColumnName, ImageFormat imageFormat, int maxWidth, int maxHeight){var dc = new DataColumn(appendSmallPicColumnName, Type.GetType("System.Byte[]"));dataTable.Columns.Add(dc);for (int i = 0; i < dataTable.Rows.Count; i++){//生成縮略圖GetSmallPic(dataTable.Rows[i], dataTableImageColumnName, appendSmallPicColumnName);var imageByte = (byte[])dataTable.Rows[i][dataTableImageColumnName];var ms = new MemoryStream(imageByte, 0, imageByte.Length);var sourceImage = Image.FromStream(ms);int newWidth, newHeight;ImageSelfAdaption(sourceImage, maxWidth, maxHeight, out newWidth, out newHeight);var myBitmap = new Bitmap(sourceImage, newWidth, newHeight);ms = new MemoryStream();myBitmap.Save(ms, imageFormat);dataTable.Rows[i][appendSmallPicColumnName] = ms.ToArray();ms.Close();}}/// <summary>/// 獲取圖像自適應后的寬高(設置最大寬高)/// </summary>/// <param name="image">圖像</param>/// <param name="maxWidth">最大寬度</param>/// <param name="maxHeight">最大高度</param>/// <param name="newWidth">自適應后的圖像寬度</param>/// <param name="newHeight">自適應后的圖像高度</param>/// <returns></returns>public static void ImageSelfAdaption(Image image, int maxWidth, int maxHeight, out int newWidth, out int newHeight){var originalWidth = image.Width;var originalHeight = image.Height;double _newWidth = maxWidth, _newHeight = maxHeight;double t = originalWidth > maxWidth ? maxWidth : originalWidth;if (originalHeight * t / originalWidth > maxHeight){_newHeight = maxHeight;_newWidth = (double)maxHeight / originalHeight * originalWidth;}else{_newWidth = t;_newHeight = (t / originalWidth) * originalHeight;}newWidth = (int)_newWidth;newHeight = (int)_newHeight;}/// <summary>/// 獲取補足透明區域的圖像/// </summary>/// <param name="image">欲補足透明區域的圖像</param>/// <param name="minComplementSize">補足透明區域的最小像素塊大小(注:必須大于等于2)</param>/// <returns></returns>public static Bitmap GetComplementImage(Image image, int minComplementSize){if (minComplementSize < 2){return new Bitmap(image.Width, image.Height);}else{int newWidth = image.Width;int newHeight = image.Height;//寬度求余int width = image.Width % minComplementSize;//高度求余int height = image.Height % minComplementSize;//寬度不夠if (width != 0){newWidth += minComplementSize - width;}if (height != 0) //高度不夠 {newHeight += minComplementSize - height;}return new Bitmap(image, newWidth, newHeight);}}/// <summary>/// 獲取圖像對象/// </summary>/// <param name="imageByte">圖像二進制數據</param>/// <returns></returns>public static Image GetImage(byte[] imageByte){if (imageByte != null){var ms = new MemoryStream();ms.Write(imageByte, 0, imageByte.Length);return Image.FromStream(ms);}return null;}/// <summary>/// 獲取圖像二進制數據/// </summary>/// <param name="image">圖像</param>/// <param name="imageFormat">圖像格式</param>/// <returns></returns>public static byte[] GetImageByteArray(Image image, ImageFormat imageFormat){var ms = new MemoryStream();image.Save(ms, imageFormat);var img = new byte[ms.Length];ms.Position = 0;ms.Read(img, 0, Convert.ToInt32(ms.Length));ms.Close();return img;}

?

轉載于:https://www.cnblogs.com/oktell/p/4601517.html

總結

以上是生活随笔為你收集整理的[转]图片处理函数(自适应缩略图datatable中添加缩略图像)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。