private void CreateSizedImage(System.Drawing.Image image, ImageSize targetSize, string targetPath) { var imageComparer = new ImageComparer(image, targetSize); ImageSize newImageSize = null; newImageSize = imageComparer.IsLandscape ? imageComparer.LandscapeSize : imageComparer.PortraitSize; using (var bitmap = new Bitmap(newImageSize.Width, newImageSize.Height, PixelFormat.Format24bppRgb)) { bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, new Rectangle(0, 0, newImageSize.Width, newImageSize.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel); } bitmap.Save(targetPath, ImageFormat.Jpeg); } }
public ImageComparer(System.Drawing.Image sourceImage, ImageSize targetSize) { this.sourceImage = sourceImage; this.targetSize = targetSize; }
public ImageDefinition(string key, int width, int height, ImageNameExtension imageNameExtension) { Key = key; Size = new ImageSize(width, height); Extension = imageNameExtension; }