CSharp examples for System.Drawing:Image Operation
Adjust Image
using System.Drawing.Drawing2D; using System.Drawing; using System.IO;// w ww . j av a2s . com using System.Collections; using System.Text; using System.Linq; using System.Collections.Generic; using System; public class Main{ public static void AdjustImage(int thumbnailSize,string fileFullPath, Stream fileContent) { Bitmap originalBMP = new Bitmap(fileContent); int newWidth, newHeight; if (originalBMP.Width > originalBMP.Height) { newWidth = thumbnailSize; newHeight = originalBMP.Height * thumbnailSize / originalBMP.Width; } else { newWidth = originalBMP.Width * thumbnailSize / originalBMP.Height; newHeight = thumbnailSize; } Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight); Graphics oGraphics = Graphics.FromImage(newBMP); oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBilinear; oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight); newBMP.Save(fileFullPath, System.Drawing.Imaging.ImageFormat.Jpeg); originalBMP.Dispose(); newBMP.Dispose(); oGraphics.Dispose(); } }