• video tutorialsWatch video tutorials to learn about Lara's tools
  • about usInformation about me, and what I am all about.
  • contactContact us for questions, suggestions and feedback
the easiest way to build professional websites
You and Lara can be friends
home     contact     sign in
  • HomeLara is a light weight content management system which is easy to use for all websites. Lara is a new web design concept which lets anyone create a unique modern web design without having to be an expert.
  • What is Lara?Learn more about what Lara can do.
  • PricingSee our prices and upgrade options
  • Start BuildingSign up to start building your website.
  • SupportView and participate in our community forums.

PNG, JPG, GIF Image Resize with .NET with transparency

north america » canada » alberta » edmonton

Proper 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
Ted - Vasastan, Stockholm - Friday, October 24, 2008 8:59 AM
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 .

Your Comment

Your Name

Your Website

Your Location

To receive emails create a username or Sign In
Your Email
Username
Password
Confirm Password
Add me to the contact list.
Remember me on this computer.
Sign in below or Sign Up
Username
Password
loading...  Post Comment
 
More
« Edit Profile and change password
Contact Box »
Related Topics
.net, png, resizing, transparency
Email to a friend
Inquire

Links

Sign Up
Pricing
Web Design for Designers and Developers

Documentation

Table of Contents

About Lara

About Us
Contact Us



Terms

Privacy Policy
Terms and Conditions

© Copyright 2010 Geographical Media Inc
Website created with Lara by Geographical Media