查了国内几个中文资料都没解决,最后是在一个英文博客上找到一个可以用的办法。
他的解决代码是:

代码
复制代码 代码如下:
//存成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="/UploadFiles/2021-04-02/gif.ashx"></body>
</html>
标签:
asp.net,透明,gif

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“asp.net 动态输出透明gif图片”

暂无“asp.net 动态输出透明gif图片”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。