Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save a gif with a transparent background?

Tags:

c#

.net

gif

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();
}
like image 493
RLH Avatar asked Jan 20 '23 09:01

RLH


1 Answers

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);
like image 91
lincolnk Avatar answered Jan 28 '23 17:01

lincolnk