实现ASP.NET动态输出透明gif图片的方法
使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF"。 下面是测试代码: Code [http://www.xueit.com] // 存成gif.ashx % @ WebHandler Language = " C# " Class = " Gif " % using System.IO; using Sy
使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF"。
下面是测试代码:

//存成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>
收藏文章
热评话题
- 在linux中导入sql文件的方法分享(使用命令行转移mysql数据库)_MySQL 技术_龙盟编程网
- 如何应用PHP函数imagettftext处理图片_php编程_个人技术分享
- 在ASP.Net中应用Javascript_Javascript编程_龙盟编程网
- 查询反向链接中link和domain的区别_SEO优化_龙盟编程网
- sql 查询本年、本月、本日记录的语句,附SQL日期函数_Sql Server开发_龙盟编程网
- Android定时器实现的几种方式整理及removeCallbacks失效问题解决_Android开发_龙盟编程网
- 如何解决C语言,函数名与宏冲突_C/C++开发_龙盟编程网
精彩图集
精彩文章
- 暂无记录。
热门标签
blog模块
指定位置
页面后退
SqlCacheDepe
5173错误
下一周
服务器名称
一点
fibonacci
browser
bean
user模型
table的tr
限速
php PHP gmda
中文问题
Reporting
加速器
password_has
javascript性能
装饰器
ArrayWritabl
linux运维
共用体
粉丝数量
XHTML
常用类
wireshark
c语
口语化
关联容器
防盗链
入门实
抓取网页
负载平衡
email
60天
合并排序
加密解密
sys密码
执行过程
标题
readdir函数
3DES
页面打印
判断网页
su
Bing图片
方程组
运行次数
故障转移
argc
已经定义
闪动
拖动
py
nextTick()
memcachedb
网络路径
SQL-UPDATE
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229