Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Example: resizing uploaded image

One of my readers asked for example about my image resizing routine. Here you can find example and also some notes about my code design solution.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

Comments

Rija said:

Hi,

This codes doesnt work.

# April 3, 2009 8:43 AM

Henry said:

Why don't you just use the built in Image.GetThumbnailImage method for creating thumbnails...?!

# April 3, 2009 8:47 AM

Petar Petrov said:

Hi.

I'm a great fan of your blog. I like your ResizeImage method but I'm also a great fan of 'using'. So I will rewrite your method as :

private void ResizeImage(double scaleFactor, Stream fromStream, Stream toStream)

{

using (var image = Image.FromStream(fromStream))

{

var newWidth = (int)(image.Width * scaleFactor);

var newHeight = (int)(image.Height * scaleFactor);

var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);

using (var thumbnailBitmap = new Bitmap(newWidth, newHeight))

{

using (var thumbnailGraph = Graphics.FromImage(thumbnailBitmap))

{

thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;

thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;

thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

thumbnailGraph.DrawImage(image, imageRectangle);

thumbnailBitmap.Save(toStream, image.RawFormat);

}

}

}

}

Because if I pass 0 as scaleFactor - newWidth and newHeight will be 0 and 0 respectively. So the constructor of Bitmap will throw an exception and image.Dispose(); will be not called. The version with 'using' doesn't have this flaw.

My 2 cents.

# April 3, 2009 9:39 AM

DigiMortal said:

Henry, my previous blog entry describes problems with GetThumbnailImage. weblogs.asp.net/.../resizing-images-without-loss-of-quality.aspx

# April 3, 2009 1:01 PM

DigiMortal said:

Thanks, Petar! Your suggestion is very good! :)

# April 4, 2009 5:39 PM

TipsOnLips.net said:

ASP.NET Image Resources

# April 5, 2009 9:38 PM

Elmer said:

This works very well. Thank you for sharing the example.

# February 4, 2010 4:06 PM

MAyur nirmal said:

Thanks a lot.

This post have helped me a lot.

This 3 line of code i was missing

thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;

   thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;

   thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

# March 12, 2010 9:12 AM

ap said:

Thanks a lot.

This post have helped me a lot.

# May 27, 2010 2:31 PM

Peter Smith said:

Hi Gunnar,

I think the possibilities with your suggested code are awesome, I just have difficulties using it. Please see  this discussion on your post here: forums.asp.net/.../4095389.aspx

If you have ANY hints/tips, help me out! :)

Hopefully I can soon implement this code in my applications, since its the best one I've seen out there!

# September 23, 2010 3:59 PM

john said:

hi gunnar ,

   i am new bie to .net......

I am getting images from webservices. when i try to resize the image, i am losing quality .. please help me for this

# October 4, 2010 12:08 AM

Adam said:

I really like this example but how would I go about saving this to file or displaying it in an image tag once it has been resized? Basically I want to attempt impliment this on a site giving the user the ability to adjust the image on the fly prior to saving to file.

# October 20, 2010 3:58 PM

Adam said:

Nevermind on my comment above I read through the forum and found your example. Thanks a bunch, this is great.

# October 20, 2010 4:30 PM

JA said:

Running the attached project file Experiments.ResizeImage.zip, the generated thumbnail if zoomed(reized) using the Windows Photo Viewer does not give the same image(original.jpg). This shows that the thumbnail has some information missing.

# October 27, 2010 5:50 PM

JA said:

Even if I resize the generated thumbnail(153 height and 204 width) with a scale factor of 10 (To generate image with same dimensions as original(2048 width and 1536 height). The new generated image is bad.

# October 27, 2010 5:59 PM

Sach said:

What are the names spaces required in order to compile the above code? I have imported System.Drawing but it is not compiling for me. And i wnat to resize image when admin uploads in my site, so how do i call the above method?

# October 29, 2010 10:10 AM

dolev pony said:

thanks for your post

finally what is the best way to resize image ? BitmapSource ?

can some one help me to find resize function in vb ?

# July 25, 2012 4:09 PM