1 public static void SaveThumbnail(Bitmap originBitmap, int width, int height, string filename) 2 { 3 using (var newImage = new Bitmap(width, height)) 4 { 5 using (var graphic = GetGraphic(originBitmap, newImage)) 6 { 7 graphic.DrawImage(originBitmap, 0, 0, width, height); 8 9 10 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); 11 ImageCodecInfo ici = null; 12 foreach (ImageCodecInfo codec in codecs) 13 { 14 if (codec.MimeType.IndexOf("png") > -1) 15 ici = codec; 16 } 17 EncoderParameters encoderParams = new EncoderParameters(); 18 long[] qualityParam = new long[1]; 19 qualityParam[0] = 100; 20 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); 21 encoderParams.Param[0] = encoderParam; 22 23 newImage.Save(filename, ici, encoderParams); 24 } 25 } 26 } 27 28 private static Graphics GetGraphic(Image originImage, Bitmap newImage) 29 { 30 newImage.SetResolution(originImage.HorizontalResolution, originImage.VerticalResolution); 31 var graphic = Graphics.FromImage(newImage); 32 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 33 graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 34 graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 35 graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 36 return graphic; 37 }
看了dudu的,但是调用报错,稍作修改
时间: 2024-12-14 18:22:47