Gunnar Peipman's ASP.NET blog

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






Resizing images without loss of quality

ASP.NET provides us with System.Drawing namespace where we can find classes we can use to manipulate with images. There are many people out there who mistakenly think that Image.GetThumbnailImage is best choice for image resizing. You can easily create crappy images if you follow the code examples in previously pointed article. In this post I will show you how to resize images without negative side effects.


NB! This blog is moved to

Click here to go to article


DigiMortal said:

Thank you for your comment, Justin. :)

I will investigate BitmapSource class as soon as I find time for it. If it makes life easier then I will blog it about for sure.

# April 2, 2009 5:19 PM

pashakasim said:

Sorry for newbie question. How would you call this class to resize to thumbnail? Thank you.

# April 2, 2009 10:10 PM

DigiMortal said:

What kind of file you were resizing (bmp/gif/jpg)?

# April 14, 2009 9:29 AM

M said:

Can you show the toStream - I get a null exception and wonder how to pass this infomation back out and post it to the page for display.

# April 21, 2009 4:53 PM

DigiMortal said:

# April 21, 2009 5:10 PM

Cyp said:

everything seems nice, but draw image sometimes doesn't resize as it should and instead of having a width of 300 for instance, you might end up with a width of 295... drawimage has some major bugs!

# June 30, 2009 7:39 AM

DigiMortal said:

Thanks for feedback, Cyp. Can you provide us with original image size and format?

# July 3, 2009 4:29 AM

nathanaeljones said:

Thanks. I've been trying to educate people about that mistake for years...

I wrote an article on this and other image resizing mistakes a while back....

I discovered a lot of them during development of my commercial image resizing module.

# August 4, 2009 5:18 PM

DigiMortal said:

Thanks for feedback, Nathanael!

I suggest also other readers take a look at links Nathanael provided here. First one is really *GOOD* reading. :)

# August 4, 2009 6:13 PM

Supratim said:

22222222222 good thanks a ton

# September 11, 2009 8:56 AM

Nayaka said:

I tried and running very well ....

Thanks GUNNAR ....

# December 10, 2009 11:18 PM

SEPEHR said:

i have a photo that is uploading like ~/images/1.jpg. how can i send this photo to this void class? in the other hand , how can i use this class? how prepare inputs (Stream)?

# December 23, 2009 4:33 AM

Bala said:

what are the namespace should be included for this method? i just convert it to the VB code. but it isshowing error on "Dim thumbnailBitmap = New Bitmap(newWidth, newHeight)". what to do ?

# January 4, 2010 12:07 AM

Maurice said:

Great post! I want to know if you can post the source code (sorry if sounds like a crazy).

# January 5, 2010 7:26 PM

Senthil C said:

Just what I was looking for... great post! Keep up the good work.

# January 10, 2010 3:27 AM

Mehul Thakkar said:

Thanks man

Great code

have been searching for this since very long time

but your blog has unique solution which was not present anywhere in the web.

Thank you very much for such a great post

# January 27, 2010 4:41 AM

Diogo said:

Thanks man!

Helps me a lot!

# February 22, 2010 6:30 PM

Simua said:

This is really very nice. I really appreciate this post.

How can I dynamically calculate the scaleFactor from the size of the original image?

This is necessary because I want my thumbnails to have a max height/width, which this example does not give.

# May 30, 2010 3:45 PM

DigiMortal said:

You can calculate it in caller method by example. The other option is to bring this calculation to resizing method. But I don't think it is an good idea because calculating scale factor is not responsibility of resize image method.

# May 30, 2010 5:15 PM

Simua said:

Thanks, I will give it a shot, I don't want to calculate it within the resize method, my calling method will calculate and pass it to the resize method.

My challenge however lies with the fomula to calculate the scale factor, given original image height, width and maximum width/height.

eg. I want to create my thumbnails to have a maximum width/height of 100 irrespective of the different sizes of images my users may upload.

I know this is not within the scope of this post, I need help on this and will really appreciate it.

# May 31, 2010 2:46 AM

Simua said:

I've gotten my answer. To calculate the scale factor of an image, you divide the max width/height by the larger of the height and width. Example (VB.NET):

Dim max As Integer = 100

If (img.Width >= img.Height) Then

   scaleFactor = (100 / img.Width)


   scaleFactor = (100 / img.Height)

End If

newWidth = CInt(img.Width * scaleFactor)

newHeight = CInt(img.Height * scaleFactor)

I hope this is helpful to someone else out there

# May 31, 2010 3:01 AM

DigiMortal said:

Thanks for your example, Simua and sorry I was not fast enough to answer (it's morning here :)). I have blog posting that you can use as an example:  It is about creatng square avatars but you can still find it useful.

# May 31, 2010 3:13 AM

Simua said:

wow, that example got a standing ovation!!!

It gave me a square thumbnail, perfect for my project.


# May 31, 2010 4:01 AM

DigiMortal said:

Be aware that squared avatars are created by cropping image if it is not square.

# May 31, 2010 4:12 AM

kazim said:

very good thanks :))

# September 26, 2010 12:36 PM

deepesh said:


I think your work is awesome, going by what i see as output and readers comments.

I tried some other examples, and i am completely new to images part, i am just a DB guy.

So can you help me in baby steps if you dont mind

I have an image ~/images/dolphin.jpg  (1024 x 800)

I need to reduce this to 100 x 100

How do i use what you have developed to do that.

You help would be very useful.

thanks a lot


# September 30, 2010 10:12 AM

DigiMortal said:

Hi Deepesh!

You can find sample solution here:

Solution is described here:

# September 30, 2010 11:10 AM

Jose Cruz, Portugal said:

Excelent, after a long research, the exact thing I needed to present a small image when I have no photo to ilustrate my articles.

Thank you

# October 18, 2010 10:16 AM

Michael said:

Have you checked out ImageSense for image resizing?  It takes care of all the edge cases and you can easily convert formats (bmp to gif to jpg to png, etc) as part of your filter rules when doing a resize event.  It also supports cropping, rotating, and color filters while preserving transparency.  

# October 26, 2010 3:30 PM

DigiMortal said:

Hi Michael! ImageSense is commercial solution but my solutions here are free :)

# October 26, 2010 8:39 PM

nwebsolution said:

Very nice example ..Excellent work

# November 30, 2010 11:37 PM

idlewild said:

Thanks.  It's the best solution so far.

# December 15, 2010 9:17 PM

Dharmendra said:

Really a very Good approach...

# December 28, 2010 7:32 AM

mohammad said:

thanks,very helpful blog

# February 15, 2011 7:30 AM

Rob said:

Damn, thanks for the help, that was a simple solution that you explained clearly.  Putting it to use now.

# April 3, 2011 6:44 PM

Juan Manuel Figueroa Bailon said:

Great article! it is exactly what i was looking for!!

By the way... let me guess that in the picture you are the one who is wearing jean jacket !!!


# April 4, 2011 11:13 AM

DigiMortal said:

Wrong guess, Juan! Try again! ;)

# April 9, 2011 3:11 AM

DigiMortal said:

Tang Si Han, you won the prize! :)

# April 16, 2011 3:01 PM

Trip Articles said:

Isn't there any easier method? Like isn't there simple software that allows you to resize and not have resolution degradation?  I am using an expensive service ShutterStock to buy my photos. I have been downloading them extra large thinking that when I "reduced" then they surely would not loose quailty. I did this size reduction in Microsoft Office Picture Manager and the result is unbelievably bad. What's going on?

# June 18, 2011 3:40 AM

DigiMortal said:

This is the method to use if you need image resizing functionality in your applications. Using command-line or COM-based external apps for this will be overkill in web context. In web systems with many users I am sure you don't want to offer image resizing functionality to users the manual way :)

# June 19, 2011 2:22 PM

johny said:

how to do it without fileupload control ? like i have the path of the file ( which is gonna be resize) so its not converting into stream but this function need stream how to resize in that case ?


# July 9, 2011 11:10 AM

Tomas Jecha MVP said:


it would be nice if you also show how to define quality of final picture (jpeg compression).

Also you should use "using" or "try-finally>Dispose" approach instead of calling "Dispose" at the end of methods. It can cause memory leaks when more larger files are processed.

# October 9, 2011 6:32 AM

Jake Molder said:

You surely deserve a round of applause for your post and more particularly, your blog in common. Really top quality material

# November 2, 2011 10:29 PM

Dhiren said:

Awesome Work Done....

Really Helped...

# December 4, 2011 1:52 PM

sad said:

Select * from Sys.Tables

Select * from Sys.Procedures

Select * from sys.functions

# February 2, 2012 12:17 AM