时间:2021-05-28
 查了国内几个中文资料都没解决,最后是在一个英文博客上找到一个可以用的办法。
他的解决代码是:
代码
复制代码 代码如下: 
//存成gif.ashx
<%@ WebHandler Language="C#" Class="Gif" %> 
using System.IO; 
using System.Web; 
using System.Drawing; 
public class Gif : IHttpHandler { 
/// <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 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); 
}
public void ProcessRequest(HttpContext context) { 
Bitmap transGif = null; 
using (Bitmap bmp = new Bitmap(300, 50)) { 
using (Graphics g = Graphics.FromImage(bmp)) { 
g.Clear(Color.Gray);
g.DrawString("transparent gif image", 
new Font("verdana bold", 14f), Brushes.LemonChiffon, 0f, 0f); 
bmp.MakeTransparent(Color.Gray);
transGif = MakeTransparentGif(bmp, Color.Black); 
}
}
if (transGif != null) { 
context.Response.Clear();
context.Response.ContentType = "image/GIF"; 
transGif.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif);
}
}
public bool IsReusable {get {return false;}} 
}
测试html文件如下
复制代码 代码如下: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<title></title>
</head>
<body style="background:#999"> 
<img src="gif.ashx" style="position:absolute" />下方的文字 
</body>
</html>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在在ASP.NET中动态的生成GIF图片一文中介绍了使用ASP.NET生成一个GIF图片的方法,本文对这个例子稍加修改就可以生成比较实用的饼图了。源代码如下:/
本文介绍了在ASP.NET中动态生成图片的方法。下面的例子是动态的生成一个显示当前时间的图片。namespaceWmj{usingSystem;usingSys
ASP.NET网站页和控件框架的建设开发ASP.NET页和控件框架是一种编程框架,它在Web服务器上运行,可以动态地生成和呈现ASP.NET网页。可以从任何浏览
本文实例讲述了ASP.NET动态添加用户控件的方法。分享给大家供大家参考。具体实现方法如下:为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGe
本文实例讲述了ASP.NET动态增加HTML元素的方法。分享给大家供大家参考,具体如下:在使用asp.net进行web开发的时候页面中的中的信息可以通过asp.