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 gunnarpeipman.com

Click here to go to article

44 Comments

  • 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.

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

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

  • 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.

  • M, please follow this link: http://weblogs.asp.net/gunnarpeipman/archive/2009/04/03/example-resizing-uploaded-image.aspx

  • 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!

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

  • 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....

    http://nathanaeljones.com/11191_20_Image_Resizing_Pitfalls

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

    http://nathanaeljones.com/products/asp-net-image-resizer/

  • Thanks for feedback, Nathanael!

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

  • 22222222222 good thanks a ton

  • I tried and running very well ....
    Thanks GUNNAR ....

  • 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)?

  • 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 ?

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

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

  • 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

  • Thanks man!
    Helps me a lot!

  • 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.

  • 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.

  • 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.

  • 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)
    Else
    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

  • 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: http://weblogs.asp.net/gunnarpeipman/archive/2009/09/16/creating-squared-avatar-images.aspx It is about creatng square avatars but you can still find it useful.

  • wow, that example got a standing ovation!!!
    It gave me a square thumbnail, perfect for my project.

    Thanxxxx.

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

  • very good thanks :))

  • Hey,

    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

    Deepesh

  • Hi Deepesh!

    You can find sample solution here: http://github.com/gpeipman/Visual-Studio-Experiments/tree/master//Experiments.ResizeImage/

    Solution is described here: http://github.com/gpeipman/Visual-Studio-Experiments/wiki/ResizeImage

  • 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

  • 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.

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

  • Very nice example ..Excellent work

  • Really a very Good approach...

  • thanks,very helpful blog

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

  • 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 !!!
    Thanks!

  • Wrong guess, Juan! Try again! ;)

  • Tang Si Han, you won the prize! :)

  • 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?

  • 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 :)

  • 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 ?
    thanks

  • Hi,
    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.

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

  • Awesome Work Done....

    Really Helped...

  • Select * from Sys.Tables

    Select * from Sys.Procedures

    Select * from sys.functions

Comments have been disabled for this content.