by sweisfeld via Shawn Weisfeld [MVP] on 6/28/2009 6:52:00 PM
I wrote this for an INETA project that I have been working on, but thought it would be great to share with everyone. We had a need to take an image and change its size. Below is an implementation of a Resize Extension Method on the .NET Image object.
I borrowed some ideas from this post by Mark McDonnell (http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx)
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Drawing;
6: using System.Drawing.Drawing2D;
7:
8: namespace Demo
9: {
10: public enum ResizeMode
11: {
12: Width,
13: Height,
14: Bigger
15: }
16:
17: public static class ImageHelper
18: {
19: public static Image Resize(this Image source, int size, ResizeMode mode)
20: {
21: //Convert the source image into a bitmap so we can work with it
22: Bitmap originalBMP = new Bitmap(source);
23: Image result;
24:
25: // Calculate the new image dimensions
26: int origWidth = originalBMP.Width;
27: int origHeight = originalBMP.Height;
28: int newWidth = 0;
29: int newHeight = 0;
30:
31: //If the size mode is set to bigger then find the widest side
32: if (mode == ResizeMode.Bigger)
33: {
34: if (origHeight > origWidth)
35: mode = ResizeMode.Height;
36: else
37: mode = ResizeMode.Width;
38: }
39:
40: //keeping the aspect ratio constant, calculate the new size
41: if (mode == ResizeMode.Width)
42: {
43: newWidth = size;
44: newHeight = (int)(size / ((double)origWidth / (double)origHeight));
45: }
46: else
47: {
48: newHeight = size;
49: newWidth = (int)(size / ((double)origHeight / (double)origWidth));
50: }
51:
52:
53: // Create a new bitmap which will hold the previous resized bitmap
54: Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
55:
56: // Create a graphic based on the new bitmap
57: using (Graphics oGraphics = Graphics.FromImage(newBMP))
58: {
59: // Set the properties for the new graphic file
60: oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
61: oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
62:
63: // Draw the new graphic based on the resized bitmap
64: oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
65:
66: // Save the new graphic file to the server
67: result = newBMP.Clone() as Image;
68:
69: // Once finished with the bitmap objects, we deallocate them.
70: originalBMP.Dispose();
71: newBMP.Dispose();
72: oGraphics.Dispose();
73: }
74:
75: return result;
76: }
77: }
78: }
Using an extension method makes it really easy to use the new “resize” functionality:
1: Image img = Image.FromFile("img.jpg");
2: Image resized = img.Resize(100, ResizeMode.Bigger);
3: resized.Save("resized.jpg");
So we went from a source size of 2304x1728 and 782 KB
to 100x75 and 23.3 KB
Now you to can shrink the size of your memories. :)
Original Post: Extension Method to Resize an Image
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.