C#中保存Gif文件设置透明无效果
来源:互联网 发布:推广发布软件 编辑:程序博客网 时间:2024/05/03 13:01
http://www.cnblogs.com/yukaizhao/archive/2008/12/18/1353745.html
/// <summary>
/// Make the image transparent.
/// The input is the color which you want to make transparent.
/// </summary>
/// <param name="color">The color to make transparent.</param>
/// <param name="bitmap">The bitmap to make transparent.</param>
/// <returns>New memory stream containing transparent background gif.</returns>
public 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);
}
- C#中保存Gif文件设置透明无效果
- C#中保存GIF文件后透明背景问题的一个解决方法
- C#中保存GIF文件后透明背景问题的最终解决方法
- C#中保存XML文件时加入头部定义行
- ScrollView影响navigationBar设置透明无效果
- 设置Gif背景透明
- c# 中如何保存透明背景的gif文件
- android xml文件中透明效果设置
- Android中保存并设置ListView位置
- C# 生成透明Gif图片
- C#取得webconfig中保存的信息
- 在properties文件中保存中文
- Oracle数据库中保存文件(C#)
- Inventor文件中保存自定义数据 - 1
- Inventor文件中保存自定义数据 - 2
- Inventor文件中保存自定义数据 - 3
- Android中保存Logcat信息到文件
- Android中保存html格式的文件
- SQL注入漏洞全接触
- Two Month Brief Notes
- Compiling the Android source code for ARMv4T
- SQL注入一日通
- 如何在sip弹起时,窗口也响应的上抬
- C#中保存Gif文件设置透明无效果
- reactos操作系统实现(16)
- bat批处理
- 算法设计方法(三)——动态规划
- gentoo mimicd+stage3
- shell编程
- 解决Vista桌面无IE问题
- 连接Oracle数据库的各种oracle JDBC驱动程序的细节
- 君子报仇,三年不晚...