asp.net 動(dòng)態(tài)輸出透明gif圖片
更新時(shí)間:2009年12月08日 01:54:47 作者:
要使用asp.net動(dòng)態(tài)輸出透明gif圖片,也就是用Response.ContentType = "image/GIF"。
查了國內(nèi)幾個(gè)中文資料都沒解決,最后是在一個(gè)英文博客上找到一個(gè)可以用的辦法。
他的解決代碼是:
代碼
//存成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>
他的解決代碼是:
代碼
復(fù)制代碼 代碼如下:
//存成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文件如下
復(fù)制代碼 代碼如下:
<!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 UpdatePanel實(shí)現(xiàn)無刷新上傳圖片
- asp.net(C#)壓縮圖片,可以指定圖片模板高寬
- ASP.NET(C#)實(shí)現(xiàn)一次性動(dòng)態(tài)上傳多張圖片的代碼(多個(gè)文件)
- Asp.Net平臺下的圖片在線裁剪功能的實(shí)現(xiàn)代碼(源碼打包)
- asp.net MVC實(shí)現(xiàn)無組件上傳圖片實(shí)例介紹
- Asp.net 2.0 無刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
- asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
- asp.net采集網(wǎng)頁圖片的具體方法
- asp.net 將一個(gè)圖片以二進(jìn)制值的形式存入Xml文件中的實(shí)例代碼
- asp.net如何在圖片上加水印文字具體實(shí)現(xiàn)
- ASP.net WebAPI 上傳圖片實(shí)例
- asp.net圖片上傳實(shí)例
- asp.net創(chuàng)建位圖生成驗(yàn)證圖片類(驗(yàn)證碼類)
- asp.net驗(yàn)證碼圖片生成示例
- ASP.NET實(shí)現(xiàn)圖片以二進(jìn)制的形式存入數(shù)據(jù)庫
- 使用asp.net改變網(wǎng)頁上圖片顏色比如灰色變彩色
- 使用asp.net改變圖片顏色如灰色的變成彩色
- Asp.net簡單實(shí)現(xiàn)給圖片增加文字水印
- ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- ASP.NET中圖片顯示方法實(shí)例
- Asp.Net上傳圖片同時(shí)生成高清晰縮略圖
- asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
- ASP.NET圖片處理三類經(jīng)典問題
相關(guān)文章
GridView自定義分頁實(shí)例詳解(附demo源碼下載)
這篇文章主要介紹了GridView自定義分頁的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了GridView自定義分頁所涉及的樣式布局及功能實(shí)現(xiàn)相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03使用visual studio自動(dòng)創(chuàng)建IIS虛擬目錄
使用visual studio自動(dòng)創(chuàng)建IIS虛擬目錄,需要的朋友可以參考一下2013-02-02Asp.Net其他頁面如何調(diào)用Web用戶控件寫的分頁
這篇文章主要介紹了Asp.Net其他頁面如何調(diào)用Web用戶控件寫的分頁,需要的朋友可以參考下2014-05-05使用SNK密鑰文件保護(hù)你的DLL和代碼不被反編譯教程
這篇文章主要介紹了使用SNK密鑰文件保護(hù)你的DLL和代碼不被反編譯教程, SNK,作為程序后綴的時(shí)候,是.net中的強(qiáng)密匙加密文件,需要的朋友可以參考下2014-09-09Asp.net ajax實(shí)現(xiàn)任務(wù)提示頁面的簡單代碼
這篇文章介紹了Asp.net ajax實(shí)現(xiàn)任務(wù)提示頁面的簡單代碼,有需要的朋友可以參考一下2013-11-11Asp.net FCKEditor 2.6.3 上傳文件沒有權(quán)限解決方法
到Fckeditor官方網(wǎng)站下載FredCK.FCKeditorV2.vs2005 (asp.net)2009-02-02