ASP. NET directly writes transparent GIF images to the source code of the output stream

Source: Internet
Author: User
Tags transparent color
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 ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.