4
votes

I have an app that allows users to create simple images that are no more than text with a desired background color. The users can select a color from a System.Windows.Forms.ColorDialog and use it to set the text color and the background color.

The background color can be set to transparent (I use Color.Transparent as the reference for the transparency) and after selection, I update the preview image which displays the text and the transparency correctly. However, when I go to save the image, I can not get the transparency to save with the image as a gif.

I found this article which states that I should use the MakeTransparent method to set the transaprency color.

Before I call the Save operation, I take the image that is in memory and redraw it using black as the background/transparent color and then before I save the image call the MakeTransperent method on the in-memory image. Still, the image saves out with black as the background.

What might I be doing wrong?


EDIT: Here is the relevant code.

This is the method that creates the image. The overrideBG variable is used to specify if we should set the transparency color to a non-alpha color for the gif.

void ReDrawImage(bool overrideBG = false) //My method that draws the image in memory.
{
    //My In Memory Image creation
    img = new Bitmap(sz.Width, sz.Height);
    Graphics gfx = Graphics.FromImage(img);

    ...

    //This portion of code sets the BG color to what should be the transparency color, if the BG is transparent
    if (overrideBG)
        {
            gfx.Clear(TransparentColor); //TransparentColor = Black, unless Text Color is Black.  If so, it equals White.
        }
        else
        {
            gfx.Clear(BackColorPreview.BackColor);
        }

    //Followed by code that writes the text.
}

//This is the save method (Assume we are always drawing a transparent background.)
Save()
{
    ReDrawImage(true);
    img.MakeTransparent(TransparentColor); //I've also tried moving this line before the ReDrawImage call
    img.Save(SaveFile.FileName, ImageFormat.Gif);
    ReDrawImage();
}
2

2 Answers

6
votes

Apparently there are issues with MakeTransparent and gifs. I found this post which provides and alternative by way of modifying the image bytes in memory. Here's the code sample from the post with syntax errors fixed.

/// <summary>  
/// Returns a transparent background GIF image from the specified Bitmap.  
/// </summary>  
/// <param name="bitmap">The Bitmap to make transparent.</param>  
/// <param name="color">The Color to make transparent.</param>  
/// <returns>New Bitmap containing a transparent background gif.</returns>  
public static Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
{
    byte R = color.R;
    byte G = color.G;
    byte B = color.B;
    MemoryStream fin = new MemoryStream();
    bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
    MemoryStream fout = new MemoryStream((int)fin.Length);
    int count = 0;
    byte[] buf = new byte[256];
    byte transparentIdx = 0;
    fin.Seek(0, SeekOrigin.Begin);
    //header  
    count = fin.Read(buf, 0, 13);
    if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null; //GIF  
    fout.Write(buf, 0, 13);
    int i = 0;
    if ((buf[10] & 0x80) > 0)
    {
        i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
    }
    for (; i != 0; i--)
    {
        fin.Read(buf, 0, 3);
        if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
        {
            transparentIdx = (byte)(256 - i);
        }
        fout.Write(buf, 0, 3);
    }
    bool gcePresent = false;
    while (true)
    {
        fin.Read(buf, 0, 1);
        fout.Write(buf, 0, 1);
        if (buf[0] != 0x21) break;
        fin.Read(buf, 0, 1);
        fout.Write(buf, 0, 1);
        gcePresent = (buf[0] == 0xf9);
        while (true)
        {
            fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
            if (buf[0] == 0) break;
            count = buf[0];
            if (fin.Read(buf, 0, count) != count) return null;
            if (gcePresent)
            {
                if (count == 4)
                {
                    buf[0] |= 0x01;
                    buf[3] = transparentIdx;
                }
            }
            fout.Write(buf, 0, count);
        }
    }
    while (count > 0)
    {
        count = fin.Read(buf, 0, 1);
        fout.Write(buf, 0, 1);
    }
    fin.Close();
    fout.Flush();
    return new Bitmap(fout);
}

I tried it with Hans' sample (replacing gif parts) and it works for me.

Your code would look like

img = MakeTransparent(img, TransparentColor); 
img.Save(SaveFile.FileName, ImageFormat.Gif);
2
votes

GDI version 1.10 supports a property to store the transparency color, property ID 20740. I see it getting used by the GIF encoder on Windows 7 with this test code:

        var bmp = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        using (var gr = Graphics.FromImage(bmp)) {
            gr.Clear(Color.Blue);
            gr.FillRectangle(Brushes.Red, new Rectangle(10, 10, 80, 80));
        }
        bmp.MakeTransparent(Color.Red);
        bmp.Save(@"c:\temp\test.gif", System.Drawing.Imaging.ImageFormat.Gif);

This code properly restores the bitmap:

    private static Bitmap LoadGif(string path) {
        var bmp = new Bitmap(path);
        bool found = false;
        foreach (System.Drawing.Imaging.PropertyItem item in bmp.PropertyItems) {
            if (item.Id == 20740) {
                int paletteIndex = item.Value[0];
                Color backGround = bmp.Palette.Entries[paletteIndex];
                bmp.MakeTransparent(backGround);
                found = true;
                break;
            }
        }
        // Property missing, punt at the color of the lower-left pixel
        //if (!found) bmp.MakeTransparent();
        return bmp;
    }

No idea if this will work on earlier versions of GDI+, I don't have XP anymore. The SDK explicitly mentions version 1.10, first shipped with Vista.