Check the domestic several Chinese materials are not resolved, and finally in an English blog to find a way to use.
His resolution code is:
Code
Copy Code code as follows:
Save into 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;}}
}
Test the HTML file as follows
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
& lt;/head>
<body style= "background: #999" >
The text below
</body>