如何保存带透明背景的gif?

RLH*_*RLH 4 .net c# gif

我有一个应用程序,允许用户创建简单的图像,只不过是具有所需背景颜色的文本.用户可以从a中选择颜色System.Windows.Forms.ColorDialog并使用它来设置文本颜色和背景颜色.

背景颜色可以设置为透明(我Color.Transparent用作透明度的参考),选择后,我更新预览图像,正确显示文本和透明度.但是,当我去保存图像时,我无法将图像保存为gif保存透明度.

我发现这篇文章指出我应该使用该MakeTransparent方法来设置transaprency颜色.

在我调用Save操作之前,我拍摄内存中的图像并使用黑色作为背景/透明颜色重绘它,然后在保存图像之前调用MakeTransperent内存中图像的方法.尽管如此,图像还是以黑色为背景.

我可能做错了什么?


编辑:这是相关的代码.

这是创建图像的方法.该overrideBG变量用于指定是否应将透明度颜色设置为gif的非alpha颜色.

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();
}
Run Code Online (Sandbox Code Playgroud)

lin*_*lnk 5

显然存在MakeTransparent与GIF 有关的问题.我发现这篇文章通过修改内存中的图像字节来提供和替代.这是修复语法错误的帖子中的代码示例.

/// <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);
}
Run Code Online (Sandbox Code Playgroud)

我用Hans的样品(替换gif部件)尝试了它,它对我有用.

你的代码看起来像

img = MakeTransparent(img, TransparentColor); 
img.Save(SaveFile.FileName, ImageFormat.Gif);
Run Code Online (Sandbox Code Playgroud)