我如何保存透明背景的GIF? [英] How can I save a gif with a transparent background?

查看:194
本文介绍了我如何保存透明背景的GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

的背景颜色可以被设置为透明的(我用 Col​​or.Transparent 作为透明度的参考)和选择后,我更新preVIEW图像,其显示文本和正确的透明度。但是,当我去保存图像,我不能让的透明度,保存图像为GIF。

我发现这篇文章其中规定,我应该使用 MakeTransparent 方法来设置transa prency颜色。

在我所说的保存操作,我认为这个是在内存中的形象和重绘采用黑色作为背景色/透明色,然后我才保存图像调用 MakeTransperent 内存中的图像的方法。尽管如此,图像保​​存了以黑色为背景。

什么可能我是做错了?


编辑:下面是相关code

这是创建图像的方法。该 overrideBG 变量用于指定是否应该设置透明色到非字母的颜色为GIF。

 无效ReDrawImage(布尔overrideBG = FALSE)//我的方法绘制图像在内存中。
{
    //我在内存映像创建
    IMG =新位图(sz.Width,sz.Height);
    显卡GFX = Graphics.FromImage(IMG);

    ...

    //将code这部分设置背景色来应该是什么颜色的透明度,如果BG是透明的
    如果(overrideBG)
        {
            gfx.Clear(TransparentColor); // TransparentColor =黑色,除非文本颜色为黑色。如果是这样,它等于白。
        }
        其他
        {
            gfx.Clear(背景色preview.BackColor);
        }

    //其次是code写入文本。
}

//这是保存方法(假设我们总是画一个透明背景。)
保存()
{
    ReDrawImage(真正的);
    img.MakeTransparent(TransparentColor); //我也试过ReDrawImage调用之前移动这条线
    img.Save(SaveFile.FileName,ImageFormat.Gif);
    ReDrawImage();
}
 

解决方案

显然有与 MakeTransparent 和GIF格式的问题。我发现<一href="https://web.archive.org/web/20131202141039/http://www.$c$cdblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/"相对=nofollow>这个帖子提供和替代通过修改图像的字节内存的方式。下面是语法错误后固定。

在code样品

  ///&LT;总结&gt;
///返回指定位图透明背景GIF图像。
///&LT; /总结&gt;
///&LT; PARAM NAME =位图&GT;的位图,使透明&LT; /参数&GT;
///&LT; PARAM NAME =色&gt;在颜色,使透明&LT; /参数&GT;
///&LT;返回&GT;新建位图包含一个透明背景的GIF&LT; /回报&GT;
公共静态位图MakeTransparentGif(位图位图,色色)
{
    字节R = color.R;
    字节G = color.G;
    字节B = color.B;
    MemoryStream的鳍=新的MemoryStream();
    bitmap.Save(翅,System.Drawing.Imaging.ImageFormat.Gif);
    MemoryStream的FOUT =新的MemoryStream((INT)fin.Length);
    诠释计数= 0;
    byte []的BUF =新的字节[256];
    字节transparentIdx = 0;
    fin.Seek(0,SeekOrigin.Begin);
    //头
    计数= fin.Read(buf中,0,13);
    如果((BUF [0] = 71)||(BUF [1] = 73)||(BUF [2] = 70)!!!)回报空; // GIF
    fout.Write(BUF,0,13);
    INT I = 0;
    如果((BUF [10]&安培; 0x80的)大于0)
    {
        I = 1&其中;&其中; ((BUF [10]及7)+1)== 256? 256:0;
    }
    对于(; i = 0;!我 - )
    {
        fin.Read(BUF,0,3);
        如果((BUF [0] == R)&安培;及(BUF [1] == G)&安培;及(BUF [2] == B))的
        {
            transparentIdx =(字节)(256  - ⅰ);
        }
        fout.Write(BUF,0,3);
    }
    布尔GCE present = FALSE;
    而(真)
    {
        fin.Read(BUF,0,1);
        fout.Write(BUF,0,1);
        如果(BUF [0] = 0×21!)破;
        fin.Read(BUF,0,1);
        fout.Write(BUF,0,1);
        GCE present =(BUF [0] == 0xf9);
        而(真)
        {
            fin.Read(BUF,0,1);
            fout.Write(BUF,0,1);
            如果(BUF [0] == 0)破;
            算的buf = [0];
            如果(fin.Read(BUF,0,计数)=计!)返回NULL;
            如果(GCE present)
            {
                如果(计数== 4)
                {
                    的buf [0] | = 0×01;
                    的buf [3] = transparentIdx;
                }
            }
            fout.Write(BUF,0,计数);
        }
    }
    而(计数大于0)
    {
        计数= fin.Read(buf中,0,1);
        fout.Write(BUF,0,1);
    }
    fin.Close();
    fout.Flush();
    返回新位图(FOUT);
}
 

我试过跟汉斯的样品(取代GIF份),它为我工作。

您code看起来像

  IMG = MakeTransparent(IMG,TransparentColor);
img.Save(SaveFile.FileName,ImageFormat.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();
}

解决方案

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);

这篇关于我如何保存透明背景的GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆