C# Bitmap/png转成jpg格式,压缩图片

时间:2024-02-24 16:25:16
public static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                    return codec;
            }
            return null;
        }

 

Bitmap im = 这里是原图片
                //转成jpg
                var eps = new EncoderParameters(1);
                var ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                eps.Param[0] = ep;
                var jpsEncodeer = GetEncoder(ImageFormat.Jpeg);
                //保存图片
                imgurl = @"/Content/Upload/" + guid + extension;
                im.Save(path + imgurl,jpsEncodeer,eps);
                //释放资源
                im.Dispose();
                ep.Dispose();
                eps.Dispose();