Krystalware
Products | Purchase | Free Stuff | Forums | Blog | Testimonials | Company | Contact

As I develop, I often have images that are bitmaps or other formats that I want to convert to transparent .png's for use in a website. I whipped up a little app that does a bmp2png conversion, using the RegistryHelper class to hook the right click context menu.

Converting an image in .NET is very straightforward, as long as you are doing a conversion from and to a .NET supported image format. Just load the image using the System.Drawing classes, and then save it as the image format you want. Making a color transparent is easy as well -- call the Bitmap.MakeTransparent method and pass in the color to make transparent.

Here's a snippet of the code I used to do the conversion and transparency settings:

using (Stream s = File.OpenRead(srcFileName))
using (Bitmap src = Image.FromStream(s, false, false) as Bitmap)
{
    src.MakeTransparent(TransparentColor);
    src.Save(destFileName, ImageFormat.Png);
}

The transparent color by default is RGB 0, 255, 128. You can change it using a appSettings key in the app.config as follows:

<add key="transparentColor" value="0, 255, 128" />

The app registers the context menu extension automatically when you run it with no arguments, and thereafter .bmp files will have a "Convert to png" right click context menu item. It would be very simple to add additional from and to formats to this code. Let me know if this is something you'd be interested in and I'll add it.

NOTE: To register the extension, administrative permissions are required.

Bmp2Png executable
Bmp2Png source

posted on Wednesday, December 20, 2006 11:25 AM | Filed Under [ Code Tools ]

Comments

Gravatar
# re: Bitmap to Png converter
Posted by Tongle
on 11/16/2007 9:48 AM
I like the idea and appreciate you code.

JPEG file is quite commonly used too. Could you add conversion of Bitmap to JPEG file?
Gravatar
# re: Bitmap to Png converter
Posted by Mark Otway
on 6/6/2008 5:58 AM
Here's an idea. Could you add a "Clipboard to PNG file" function?

I frequently have to send screenshots to email lists I'm on. I hate sending BMPs, so I do this:

- Select Window
- Alt-PrtScrn to copy screenshot to clipboard
- Run pbrush.exe
- Paste
- save as PNG
- attach to email

Would be really neat to just do

- Select Window
- Alt-PrtScrn
- Right-click desktop, choose 'Paste to PNG'
- File created on desktop called Screen005.png

:)
Title  
Name
Email (never displayed)
Url
Comments   
Please add 4 and 5 and type the answer here: