zoukankan      html  css  js  c++  java
  • asp.net动态输出透明gif图片

    要使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF"。

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

    http://www.codedblog.com/2007/08/28/generating-a-transparent-gif-image-using-c/

    他的解决代码是:

    复制代码
    代码
    //存成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, 013);
            
    if ((buf[0!= 71|| (buf[1!= 73|| (buf[2!= 70)) return null//GIF
            fout.Write(buf, 013);
            
    int i = 0;
            
    if ((buf[10& 0x80> 0) {
                i 
    = 1 << ((buf[10& 7+ 1== 256 ? 256 : 0;
            }
            
    for (; i != 0; i--) {
                fin.Read(buf, 
    03);
                
    if ((buf[0== R) && (buf[1== G) && (buf[2== B)) {
                    transparentIdx 
    = (byte)(256 - i);
                }
                fout.Write(buf, 
    03);
            }
            
    bool gcePresent = false;
            
    while (true) {
                fin.Read(buf, 
    01);
                fout.Write(buf, 
    01);
                
    if (buf[0!= 0x21break;
                fin.Read(buf, 
    01);
                fout.Write(buf, 
    01);
                gcePresent 
    = (buf[0== 0xf9);
                
    while (true) {
                    fin.Read(buf, 
    01);
                    fout.Write(buf, 
    01);
                    
    if (buf[0== 0break;
                    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, 01);
                fout.Write(buf, 
    01);
            }
            fin.Close();
            fout.Flush();
            
    return new Bitmap(fout);
        }

        
    public void ProcessRequest(HttpContext context) {
            Bitmap transGif 
    = null;
            
    using (Bitmap bmp = new Bitmap(30050)) {
                
    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>
  • 相关阅读:
    http协议基础(三)几种数据传输方式
    http协议基础(二)请求和响应报文的构成
    http协议(一)基础知识
    性能测试常见术语浅析
    TPS和QPS定义以及影响TPS的因素
    JMeterPlugin性能监控
    jmeter压测之 监控--nmon
    对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
    C# AES的128位、192位、256位加密
    C# 处理base64 以及base64的原理分析
  • 原文地址:https://www.cnblogs.com/wangluochong/p/3622386.html
Copyright © 2011-2022 走看看