PNG, JPG, GIF Image Resize with .NET with transparencyProper resizing of images in .net can be accomplished fairly easily, but you must be careful to maintain the original pixel formats as well as the original raw format of the image. By maintaining the original pixel formats you can use the GDI Canvas and Bitmap objects to properly resize an image without loss of transparency or quality. The key to success in this method (and how it differs from the microsoft examples) is in the declaration of the Bitmap object with the pixel format parameter as the OriginalImage pixel format. The following code has been tested with transparent PNGs, GIFs and with JPG formats and should work for other formats as well. Would you like to comment?There are 12 comments - add your comment It works! Thank you! Anz - Asia, World - Sunday, November 02, 2008 5:18 AM Thanks for the code.. but can you please show me how can I use it along with an asp:upload.. I mean how can I use for an image file before its actually saving to the server. Anonymous - Monday, January 19, 2009 10:14 PM nice Terry - Singapore, Asia - Thursday, March 05, 2009 10:12 PM hi, i tried passing in a transparent gif, but it throws an exception: A Graphics object cannot be created from an image that has an indexed pixel format.
I tried to declare the Bitmap Object with the Format variable but the transparency does not work. adela - Sunday, March 15, 2009 5:44 AM Nice! also you can read about image resizing without losing the quality here: http://www.csnotes.net/?p=66 Rohan Patel - Mumbai, India - Thursday, March 19, 2009 7:51 AM Hi, I have search and seen lots of methods to resize images but all of them were not this much simple and accurate. Mainly i wanted to resize transparent PNG images. Your code works great for all. Thanks a ton. PK - Australia, Oceania - Wednesday, March 25, 2009 9:50 PM I think there might be a bug in the code. your code: using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat)) should it be: using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, Format)) *notice the last argument on the constructor* this might also help Terry from SG. maybe. i've not tested, yet. Also, naming standard is phail -- first character for local scop variables should be lower case ... but this is a religious war i'm not gonna start :) nice work nonetheless ;) Adam Cohen - San Francisco, California - Friday, May 22, 2009 3:19 PM I got the same exception as Terry when I used a transparent gif. Anyone have an idea? Kim - Arhus, Denmark - Tuesday, June 16, 2009 1:27 PM I got the same exception as Terry when I used a transparent gif. Making PK's modification eliminates the exception, but transparency is lost on both gif's and png's. Meron Lavie - Asia, World - Thursday, September 17, 2009 7:18 AM I got Terry and Kim's problem, too. Anyone solve this? Dani - Asia, World - Tuesday, September 22, 2009 12:36 PM The best !!! Mnh - Asia, World - Thursday, September 02, 2010 8:38 AM Thanks so much , it is useful for me .
|