Writing an image to the HTTP-response output stream is very simple, but it is difficult to write a transparent GIF image to the output stream. This Program In ASP. NET, use the C # language to create a color palette first, and then change all alpha values to 0. Then, the transparent color in the GIF will be transparent.
System. Drawing. Image _ gifimage;
_ Gifimage = system. Drawing. image. fromfile (server. mappath ("White. jpg "));
Bitmap Bm = new Bitmap (_ gifimage. Width, _ gifimage. height,
Pixelformat. format8bppindexed );
// Get the palette from the bitmap
Colorpalette pal = BM. palette;
// Set Alpha to 0
For (INT I = 0; I <pal. Entries. length; I ++)
{
Color Col = pal. Entries [I];
Pal. Entries [I] = color. fromargb (0, col. R, col. G, col. B );
}
// Assign palette
BM. palette = pal;
// To copy the bitmap data we need to lock the Source &
// Destination bits
Bitmapdata src = (Bitmap) _ gifimage). lockbits (New rectangle
(0, 0, _ gifimage. Width, _ gifimage. Height), imagelockmode. readonly,
_ Gifimage. pixelformat );
Bitmapdata DST = BM. lockbits (New rectangle (0, 0, BM. Width, BM. Height ),
Imagelockmode. writeonly, BM. pixelformat );
// Finished, unlock the bits
(Bitmap) _ gifimage). unlockbits (SRC );
BM. unlockbits (DST );
// Set response type to "image/GIF"
Response. contenttype = "image/GIF ";
// Writing the GIF directly into the output-stream
BM. Save (response. outputstream, imageformat. GIF );
// Cleaning up
BM. Dispose ();
_ Gifimage. Dispose ();
// Send output stream
Response. Flush ();