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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

C#实现图片的无损压缩

發(fā)布時間:2023/12/13 综合教程 21 生活家
生活随笔 收集整理的這篇文章主要介紹了 C#实现图片的无损压缩 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/// <summary>

/// 圖像縮略圖處理

/// </summary>

/// <param name="bytes">圖像源數據</param>

/// <param name="compression">壓縮質量 1-100</param>

/// <param name="thumbWidth">縮略圖的寬</param>

/// <param name="thumbHeight">縮略圖的高</param>

/// <returns></returns>

public static byte[] ConvertToThumbnail(byte[] bytes, int compression = 100, int thumbWidth = 0, int thumbHeight = 0)

{

byte[] bs = null;

try

{

if (bytes != null)

{

using (MemoryStream ms = new MemoryStream(bytes))

{

using (Bitmap srcimg = new Bitmap(ms))

{

if (thumbWidth == 0 && thumbHeight == 0)

{

thumbWidth = srcimg.Width;

thumbHeight = srcimg.Height;

}

using (Bitmap dstimg = new Bitmap(thumbWidth, thumbHeight))//圖片壓縮質量

{

//從Bitmap創(chuàng)建一個System.Drawing.Graphics對象,用來繪制高質量的縮小圖。

using (Graphics gr = Graphics.FromImage(dstimg))

{

//把原始圖像繪制成上面所設置寬高的縮小圖

Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight);

gr.Clear(Color.WhiteSmoke);

gr.CompositingQuality = CompositingQuality.HighQuality;

gr.SmoothingMode = SmoothingMode.HighQuality;

gr.InterpolationMode = InterpolationMode.HighQualityBicubic;

gr.DrawImage(srcimg, rectDestination, 0, 0, srcimg.Width, srcimg.Height, GraphicsUnit.Pixel);

EncoderParameters ep = new EncoderParameters(1);

ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);//設置壓縮的比例1-100

ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();

ImageCodecInfo jpegICIinfo = arrayICI.FirstOrDefault(t => t.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid);

using (MemoryStream dstms = new MemoryStream())

{

if (jpegICIinfo != null)

{

dstimg.Save(dstms, jpegICIinfo, ep);

}

else

{

dstimg.Save(dstms, System.Drawing.Imaging.ImageFormat.Png);//保存到內存里

}

bs = new Byte[dstms.Length];

dstms.Position = 0;

dstms.Read(bs, 0, bs.Length);

}

}

}

}

}

}

}

catch (Exception ex)

{

LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), string.Concat("ConvertToThumbnail error.", thumbWidth, " ", thumbHeight));

}

return bs;

}

總結

以上是生活随笔為你收集整理的C#实现图片的无损压缩的全部內容,希望文章能夠幫你解決所遇到的問題。

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