C#生成透明Gif图片

C#生成透明Gif图片


2023年12月26日发(作者:)

C#生成透明Gif图片

C#中生成透明Gif文件

在C#中使用(arent)生成gif格式透明文件时,设置透明不起作用。使用ansparent 方法同样也不能使gif文件透明。这两个问题也是微软的设计使然,和gif文件格式有关系。

如果要在c#中编程实现透明色,必须另写代码,下面是一位老外的代码

-->///

/// Make the image transparent.

/// The input is the color which you want to make transparent.

///

/// The color to make

transparent.

/// The bitmap to make

transparent.

/// New memory stream containing transparent

background gif.

public Bitmap MakeTransparentGif(Bitmap bitmap, Color

color)

{

byte R = color.R;

byte G = color.G;

byte B = color.B;

MemoryStream fin = new MemoryStream();

(fin, );

MemoryStream fout = new MemoryStream((int));

int count = 0;

byte[] buf = new byte[256];

byte transparentIdx = 0;

(0, );

//header

count = (buf, 0, 13);

if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null;

//GIF

(buf, 0, 13);

int i = 0;

if ((buf[10] & 0x80) > 0)

{

i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;

}

for (; i != 0; i--)

{

(buf, 0, 3);

if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))

{

transparentIdx = (byte)(256 - i);

}

(buf, 0, 3);

}

bool gcePresent = false;

while (true)

{

(buf, 0, 1);

(buf, 0, 1);

if (buf[0] != 0x21) break;

(buf, 0, 1);

(buf, 0, 1);

gcePresent = (buf[0] == 0xf9);

while (true)

{

(buf, 0, 1);

(buf, 0, 1);

if (buf[0] == 0) break;

count = buf[0];

if ((buf, 0, count) != count) return null;

if (gcePresent)

{

if (count == 4)

{

buf[0] |= 0x01;

buf[3] = transparentIdx;

}

}

(buf, 0, count);

}

}

while (count > 0)

{

count = (buf, 0, 1);

(buf, 0, 1);

}

();

();

return new Bitmap(fout);

}

在生成图片之后调用此方法,即可使gif显示透明色。


发布者:admin,转转请注明出处:http://www.yc00.com/news/1703599174a1305299.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信