2011-06-27 32 views
4

我有一个应用程序,允许用户创建不超过带有所需背景色的文本的简单图像。用户可以从System.Windows.Forms.ColorDialog中选择一种颜色,并用它来设置文本颜色和背景颜色。如何保存带有透明背景的gif?

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

我发现this article其中规定我应该使用MakeTransparent方法来设置透明度颜色。

在我调用保存操作之前,我将内存中的图像作为背景/透明颜色使用黑色重新绘制,然后在保存图像之前,在内存图像上调用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(); 
} 
+0

发布您的代码。 – lincolnk

回答

3

显然有MakeTransparent和gif问题。我发现this post它提供并通过修改内存中的图像字节的方式替代。这是来自帖子的代码示例,其中修复了语法错误。

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

我试过用汉斯的样品​​(取代gif零件),它适用于我。

您的代码看起来像

img = MakeTransparent(img, TransparentColor); 
img.Save(SaveFile.FileName, ImageFormat.Gif); 
+0

这不是我希望的答案,但它是一个工作的。谢谢。 – RLH

+0

它不在我身边工作:( – Nofuzy

2

GDI版本1.10支持存储透明度颜色的属性,属性ID 20740.我看到它习惯用GIF编码器在Windows 7上使用此测试代码:

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

此代码正确恢复位图:

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

不知道这是否适用于早期版本的GDI +,我没有XP了。 SDK明确提到版本1.10,首先随Vista发货。

+0

这实际上就是我在做的,只是我的过程被分割到多个方法中。另外,我不需要将图像恢复到应用程序。 – RLH

+1

我不知道这是什么意思。无论你用什么程序来查看图像似乎都不使用属性项。尝试另一个或写你自己的。 –

+0

我创建的图像存储在显示在PictureBox中的内存中。当用户保存图像时,我将内存中的图像(img)写入gif文件。我的过程与第一个代码示例中所述的完全相同,不同之处在于我在ReDrawImage方法中操纵图像,并将该文件保存在Save方法中。 – RLH