Mega Code Archive

 
Categories / C# / 2D Graphics
 

Resize Image

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; namespace Phaeton.Helpers.ImageUtilities {     public static class ImageExtensions     {         public static void SetResizedImage(this Image image, Image sourceImage)         {             GraphicsUnit sourceUnit = GraphicsUnit.Pixel;             GraphicsUnit destinationUnit = GraphicsUnit.Pixel;             Graphics.FromImage(image).DrawImage(sourceImage, image.GetBounds(ref destinationUnit),                 sourceImage.GetBounds(ref sourceUnit), GraphicsUnit.Pixel);         }         public static Image ResizeDownToHeight(this Image image, int height)         {             if (image.Height <= height)             {                 return image;             }             Image resizedImage = new Bitmap(height * image.Width / image.Height, height);             resizedImage.SetResizedImage(image);                         return resizedImage;         }         public static Image ResizeDownToWidth(this Image image, int width)         {             if (image.Width <= width)             {                 return image;             }             Image resizedImage = new Bitmap(width, width * image.Height / image.Width);             resizedImage.SetResizedImage(image);             return resizedImage;         }     } }