public void SendSmallImage(string fileName, int maxHeight, int maxWidth) { Image image = null; this._outBmp = null; Graphics graphics = null; try { image = Image.FromFile(fileName); Size size = PIC.NewSize(maxWidth, maxHeight, image.Width, image.Height); this._outBmp = new Bitmap(size.Width, size.Height); graphics = Graphics.FromImage(this._outBmp); graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); if (graphics != null) { graphics.Dispose(); } } catch { } finally { if (graphics != null) { graphics.Dispose(); } if (image != null) { image.Dispose(); } } }
public static void SendSmallImage(string fileName, string newFile, int maxHeight, int maxWidth) { Image image = null; Bitmap bitmap = null; Graphics graphics = null; try { image = Image.FromFile(fileName); ImageFormat rawFormat = image.RawFormat; Size size = PIC.NewSize(maxWidth, maxHeight, image.Width, image.Height); bitmap = new Bitmap(size.Width, size.Height); graphics = Graphics.FromImage(bitmap); graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); if (graphics != null) { graphics.Dispose(); } EncoderParameters encoderParameters = new EncoderParameters(); long[] value = new long[] { 100L }; EncoderParameter encoderParameter = new EncoderParameter(Encoder.Quality, value); encoderParameters.Param[0] = encoderParameter; ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo imageCodecInfo = null; for (int i = 0; i < imageEncoders.Length; i++) { if (imageEncoders[i].FormatDescription.Equals("JPEG")) { imageCodecInfo = imageEncoders[i]; break; } } if (imageCodecInfo != null) { bitmap.Save(newFile, imageCodecInfo, encoderParameters); } else { bitmap.Save(newFile, rawFormat); } } catch { } finally { if (graphics != null) { graphics.Dispose(); } if (image != null) { image.Dispose(); } if (bitmap != null) { bitmap.Dispose(); } } }