当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

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, 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];

相关内容
赞助商链接