Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Resizing an Image in C#

3/26/2008

One of the things that comes up constantly for some reason is image manipulation and specifically resizing of an image. It's actually really simple to do. However by default resizing an image in .Net is done using a fast method that tends to have a poor quality. We can, however, set the quality of the function based on our needs. Anyway here's the code to do it:

   1: /// <summary>
   2: /// Resizes an image to a certain height
   3: /// </summary>
   4: /// <param name="Image">Image to resize</param>
   5: /// <param name="Width">New width for the final image</param>
   6: /// <param name="Height">New height for the final image</param>
   7: /// <param name="Quality">Quality of the resizing</param>
   8: /// <returns>A bitmap object of the resized image</returns>
   9: public static Bitmap ResizeImage(Bitmap Image, int Width, int Height, Quality Quality)
  10: {
  11:     Bitmap NewBitmap = new Bitmap(Width, Height);
  12:     using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
  13:     {
  14:         if (Quality == Quality.High)
  15:         {
  16:             NewGraphics.CompositingQuality = CompositingQuality.HighQuality;
  17:             NewGraphics.SmoothingMode = SmoothingMode.HighQuality;
  18:             NewGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  19:         }
  20:         else
  21:         {
  22:             NewGraphics.CompositingQuality = CompositingQuality.HighSpeed;
  23:             NewGraphics.SmoothingMode = SmoothingMode.HighSpeed;
  24:             NewGraphics.InterpolationMode = InterpolationMode.NearestNeighbor;
  25:         }
  26:         NewGraphics.DrawImage(Image, new System.Drawing.Rectangle(0, 0, Width, Height));
  27:     }
  28:     return NewBitmap;
  29: }

The Quality item is a basic enum:

   1: public enum Quality
   2: {
   3:     High,
   4:     Low
   5: }

As you can see, it's rather simple to accomplish, but is definitely one of those items that should be in every web developers tool box. All the code does, is creates a new bitmap, sets the quality settings for the resize, and then lets .Net resize the image for us. Anyway, try out the code, leave comments, and happy coding.



Comments