C# 如何将gif保存为透明背景?
我有一个应用程序,允许用户创建简单的图像,这些图像只不过是带有所需背景颜色的文本。用户可以从C# 如何将gif保存为透明背景?,c#,.net,gif,C#,.net,Gif,我有一个应用程序,允许用户创建简单的图像,这些图像只不过是带有所需背景颜色的文本。用户可以从System.Windows.Forms.ColorDialog中选择颜色,并使用它设置文本颜色和背景颜色 背景色可以设置为透明(我使用color.transparent作为透明度的参考),选择后,我会更新正确显示文本和透明度的预览图像。但是,当我去保存图像时,我无法获得将图像保存为gif的透明度 我找到了应该使用该方法设置transaprency颜色的状态 在调用Save操作之前,我获取内存中的图像,并
System.Windows.Forms.ColorDialog
中选择颜色,并使用它设置文本颜色和背景颜色
背景色可以设置为透明(我使用color.transparent
作为透明度的参考),选择后,我会更新正确显示文本和透明度的预览图像。但是,当我去保存图像时,我无法获得将图像保存为gif的透明度
我找到了应该使用该方法设置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();
}
GDI版本1.10支持存储透明度颜色的属性,属性ID 20740。我看到它被Windows 7上的GIF编码器使用,测试代码如下:
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明确提到了Vista的第一个版本1.10。显然,
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);
}
这基本上就是我正在做的,只是我的过程被分割成多个方法。另外,我不需要将图像恢复到应用程序中。我不知道这到底意味着什么。无论您使用什么程序查看图像,都似乎没有使用属性项。我创建了一个存储在内存中的图像,它显示在一个PictureBox中。当用户保存图像时,我将内存中的图像(img)写入gif文件。我的过程与您在第一个代码示例中概述的完全相同,除了我在重画图像方法中操作图像,并在保存方法中保存文件外,我在WinXP上测试了第一个代码段,但它似乎不起作用-我用chrome查看时得到一个带蓝色边框的黑色正方形。它不起作用:
paletteIndex
超出paletteIndex.Entries[paletteIndex]的范围
这不是我所希望的答案,但它确实有效。非常感谢。
img = MakeTransparent(img, TransparentColor);
img.Save(SaveFile.FileName, ImageFormat.Gif);