Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a. Net compressed bitmap to JPEG code

Shulou Source: shulou.com Published: 2022-06-03 07:40:22 10月04日 Update

This article focuses on "how to write a. Net compressed bitmap to JPEG code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write a. Net compressed bitmap to JPEG code.

First prepare a bitmap image source.bmp and save it in the same directory as bmp.aspx

The copy code is as follows:

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

{

/ / set the mime type to image/jpeg, and output the image in JPGE format to the browser soon.

Response.Clear ()

Response.ContentType= "image/jpeg"

Bitmap OutputBitmap = new Bitmap (Server.MapPath ("source.bmp")); / / New 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 100Max quality is 100mm

System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter (System.Drawing.Imaging.Encoder.Quality, quality)

EncoderParams.Param [0] = encoderParam

ImageCodecInfo [] arrayICI = ImageCodecInfo.GetImageEncoders (); / / gets the ImageCodecInfo object that contains information about the built-in image codec.

ImageCodecInfo jpegICI = null

For (int x = 0; x < arrayICI.Length; x +)

{

If (arrayICI [x] .FormatDescription.Equals ("JPEG"))

{

JpegICI = arrayICI [x]; / / set JPEG encoding

Break

}

}

If (jpegICI! = null)

{

OutputBitmap.Save (Response.OutputStream, jpegICI, encoderParams); / / saves bitmap objects in stream format and JPEG codec parameters to the output stream.

}

/ / clean up

OutputBitmap.Dispose ()

}

Enter: http://localhost/bmp.aspx?comp=0 in the browser address

You will see the image, adjust the value of comp, and you will see a different effect.

At this point, I believe you have a deeper understanding of "how to write a. Net compressed bitmap to JPEG code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Code bitmap image object content format browser coding learning browsing output different practical deeper highest information interest parameter address practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Huawei vpn Docker