时间:2016-02-15 22:05 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net 动态输出透明gif图片】,下面是详细的讲解!
asp.net 动态输出透明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;}}
}
关于asp.net 动态输出透明gif图片的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【页面打印】关于ASP.NET页面打印技术的常用方法
- 【MVC5】MVC 5 第一章 创建MVC 5 web应用程序-net
- 【MVC】一个简单MVC5 + EF6示例分享-EF6实例-MVC5
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【asp】asp.net更新指定记录的方法-net--更新-指
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
