本文主要是介绍c# 图片压缩算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#region 图片压缩private static byte[] CompressionImage(Stream fileStream, long quality){using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream)){using (Bitmap bitmap = new Bitmap(img)){ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;EncoderParameters myEncoderParameters = new EncoderParameters(1);EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);myEncoderParameters.Param[0] = myEncoderParameter;using (MemoryStream ms = new MemoryStream()){bitmap.Save(ms, CodecInfo, myEncoderParameters);myEncoderParameters.Dispose();myEncoderParameter.Dispose();return ms.ToArray();}}}}private static ImageCodecInfo GetEncoder(ImageFormat format){ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();foreach (ImageCodecInfo codec in codecs){if (codec.FormatID == format.Guid){ return codec; }}return null;}#endregion
这篇关于c# 图片压缩算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!