views:

743

answers:

3

So i am converting a metafile (EMF to be exact) to a jpeg or gif (doesn't matter as long as it's compatible with browsers) and when I do the conversion, all of the transparent pixels turn black. I have no idea how to do this in GDI+ but here is the method I am using to save the file:

Dim Img As System.Drawing.Imaging.Metafile = New System.Drawing.Imaging.Metafile(stream)
Img.Save(Server.MapPath("/FileName.gif"), System.Drawing.Imaging.ImageFormat.Gif)
A: 

My understanding is that you need to provide information to the framework, regarding the image format of the destination AND source images in order for alpha channels to be translated correctly.

Specifically color information: bits per pixel and whether or not there is per-pixel alpha or a transparent color or a single alpha value for the entire image - stuff like that.

However, I'm not certain how this is done in VB (or in .NET at all for that matter) - I use native C++ for these types of operations ...

Giffyguy
+4  A: 

check out Bob Powel's web site. he has a metric-crap-ton of GDI+ stuff. this one is specifically for trans GIF's

Muad'Dib
A: 

As I understand it the metafile does not contain information for the background - it is simply transparent.

A simple work around is to simply colour the background the colour you want.

E.g. The code below creates a bitmap the same size as the original image, draws the background white and then draws the metafile image on top of the background.

        Rectangle imageRect = new Rectangle(0, 0, originalImage.Width, originalImage.Height);
        Bitmap newImage = new Bitmap(originalImage.Width, originalImage.Height);
        Graphics g = Graphics.FromImage(newImage );
        g.FillRectangle(new SolidBrush(Color.White), imageRect);
        g.DrawImage(originalImage, new Point(0, 0));
Kildareflare