asp.net动态输出透明gif图片
要使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF"。
查了国内几个中文资料都没解决,最后是在一个英文博客上找到一个可以用的办法。
http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/
他的解决代码是:

代码
<%@ 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 = ;
byte[] buf = new byte[];
byte transparentIdx = ;
fin.Seek(, SeekOrigin.Begin);
//header
count = fin.Read(buf, , );
if ((buf[] != ) || (buf[] != ) || (buf[] != )) return null; //GIF
fout.Write(buf, , );
int i = ;
if ((buf[] & 0x80) > ) {
i = << ((buf[] & ) + ) == ? : ;
}
for (; i != ; i--) {
fin.Read(buf, , );
if ((buf[] == R) && (buf[] == G) && (buf[] == B)) {
transparentIdx = (byte)( - i);
}
fout.Write(buf, , );
}
bool gcePresent = false;
while (true) {
fin.Read(buf, , );
fout.Write(buf, , );
if (buf[] != 0x21) break;
fin.Read(buf, , );
fout.Write(buf, , );
gcePresent = (buf[] == 0xf9);
while (true) {
fin.Read(buf, , );
fout.Write(buf, , );
if (buf[] == ) break;
count = buf[];
if (fin.Read(buf, , count) != count) return null;
if (gcePresent) {
if (count == ) {
buf[] |= 0x01;
buf[] = transparentIdx;
}
}
fout.Write(buf, , count);
}
}
while (count > ) {
count = fin.Read(buf, , );
fout.Write(buf, , );
}
fin.Close();
fout.Flush();
return new Bitmap(fout);
}
public void ProcessRequest(HttpContext context) {
Bitmap transGif = null;
using (Bitmap bmp = new Bitmap(, )) {
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>
asp.net动态输出透明gif图片的更多相关文章
- asp.net 动态压缩、切割图片,并做缓存处理机制
在asp.net中,新建一个handler,把需要切割的网内图片,通过调用此URL来切割并缓存.http://localhost:53829/CacheImage/ResizeImage.ashx?s ...
- PHP 动态输出 svgz 格式图片
使用PHP动态生成SVGZ图片(gzip压缩的SVG) 经测试SVG的动画性能很差,简单的动画CPU都能占到 30%左右. 可能的用途: 动态天气图片 访问统计计数图片 文字验证生成 动态头像 静态外 ...
- php 合并图片 (将活动背景图片和动态二维码图片合成一张图片)
<?php //案例一:将活动背景图片和动态二维码图片合成一张图片 //图片一 $path_1 = './background.png'; //图片二 $path_2 = './FU0851_2 ...
- ASP.NET 动态编译、预编译和 WebDeployment 项目(转)
概述 在 Web 服务器上,既可以部署源文件,也可以部署编译后程序集. 若部署源文件,则当用户访问时,Web 应用程序会被动态编译,并缓存该程序集,以便下次访问. 否则,若部署程序集,Web 应用程序 ...
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌图片:自己的图片控件
代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ...
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
在"MVC文件图片ajax上传轻量级解决方案,使用客户端JSAjaxFileUploader插件01-单文件上传"一文中,使用JSAjaxFileUploader这款插件实现了单文 ...
- 【转载】使用Response.WriteFile输出文件以及图片
Response对象是Asp.Net应用程序中非常重要的一个内置对象,其作用为负责将服务器执行好的信息输出给客户端,可以使用Response.WriteFile方法来像客户端输出文件或者图片,输出图片 ...
- 利用box-shadow实现伪边框透明到图片
前阵子突然看到了一个效果,一张图片,有一个边框,但是边框可以透明到图片.直接上图 贴代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- asp.net 动态添加自定义控件
前两天一直纠结asp.net动态添加控件后,后台获取不到控件的问题,查看了网上很多的回答,可能自己的理解有误或者自己所掌握的知识有限,都没有解决我遇到的问题,经过两天的研究,终于把问题解决了. 我这里 ...
随机推荐
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- 页面无法访问 css文件加载问题
1.青奥项目的web配置: 后缀为.html和.vm的请求会被控制器拦截. 控制器没设置目标资源,所以无法访问到资源! 2.文件不能放在vm文件夹下,因为设置了视图解析,vm文件夹下的文件只有后缀为 ...
- tomcat发布项目时,localhost可以访问,用ip地址不能访问,同时其他电脑也不能访问。
最近在开发项目中,遇到的一个问题是:在 tomcat中发布一个web项目,但是发布成功后,只能用http://localhost:8080/aml访问项目,不能用http://172.0.0.1:80 ...
- java操作word
一个使用Apache POI写word文档的实例: 1 package apache.poi; 2 3 import java.io.ByteArrayInputStream; 4 import ja ...
- 输入年月,输出月份有几天(分别用了if——else和switch)
首先是switch做的 class Program { static void Main(string[] args) {/* 题目要求:请用户输入年份,输入月份,输出该月的天数. 思路:一年中月份的 ...
- 解决vs2015使用fopen、fprintf等函数报错的问题
出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...
- NOIP 2012 T5 借教室 [洛谷P1083]
题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自 ...
- groovy-保留字
groovy的保留字: abstractasassertbooleanbreakbytecasecatchcharclassconstcontinuedefdefaultdodoubleelseenu ...
- OAuth2.0认证和授权原理
什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...