Example code for compressing A. net bitmap to JPEG

Source: Internet
Author: User

First, upload a map as source.bmp and save it in the same directory as bmp. aspx.
Copy codeThe Code is as follows:
<% @ Page language = "c #" %>
<% @ Import Namespace = "System. Drawing" %>
<% @ Import Namespace = "System. Drawing. Imaging" %>



<Script language = "c #" runat = "server">

Private void Page_Load (object sender, System. EventArgs e)
{

// Set the mime type to image/jpeg. The JPGE format image will be output to the browser.
Response. Clear ();
Response. ContentType = "image/jpeg ";


Bitmap OutputBitmap = new Bitmap (Server. MapPath ("source.bmp"); // create a BitMap object
System. Drawing. Imaging. EncoderParameters encoderParams = new System. Drawing. Imaging. EncoderParameters ();
Long [] quality = new long [1];

Int comp = 0;
If (Request. QueryString ["comp"]! = "") {Comp = Convert. ToInt16 (Request. QueryString ["comp"]);}
Quality [0] = comp; // 0 to 100 the highest quality is 100
System. Drawing. Imaging. EncoderParameter encoderParam = new System. Drawing. Imaging. EncoderParameter (System. Drawing. Imaging. Encoder. Quality, quality );
EncoderParams. Param [0] = encoderParam;

ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders (); // obtain the ImageCodecInfo object that contains information about the built-in image decoder.
ImageCodecInfo policici = null;
For (int x = 0; x <arrayICI. Length; x ++)
{
If (arrayICI [x]. FormatDescription. Equals ("JPEG "))
{
JpegICI = arrayICI [x]; // sets the JPEG encoding.
Break;
}
}

If (policici! = Null)
{
OutputBitmap. Save (Response. OutputStream, jpegICI, encoderParams); // saves the bitmap object to the output stream in the stream format and uses the JPEG codec parameter.

}

// Clean up
OutputBitmap. Dispose ();

}
</Script>

Enter http: // localhost/bmp. aspx in the browser address? Comp = 0
The image is displayed, and the value of comp is adjusted. Different effects are displayed.

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.