. Net image scaling Quality

Source: Internet
Author: User
A long time ago, I completed a general class to process. Net image scaling and watermarks. To be honest, I have never paid attention to image quality issues. Till recently, someone finally pointed it out to me Program The quality of the generated image is not high (they always enlarge the image to 200% and 300% to view details ). The source image is as follows:

Now we want to scale to make the height fixed at 750px

1. Earliest correlation Code : Bitmap IMG = new Bitmap (object_width, object_height );
IMG. setresolution (72f, 72f );
Graphics gdiobj = graphics. fromimage (IMG );
Gdiobj. compositingquality = compositingquality. highquality;
Gdiobj. smoothingmode = smoothingmode. highquality;
Gdiobj. interpolationmode = interpolationmode. highqualitybicubic;
Gdiobj. pixeloffsetmode = pixeloffsetmode. highquality;
Gdiobj. fillrectangle (New solidbrush (color. White), 0, 0,
Object_width, object_height );
Rectangle destrect = new rectangle (0, 0,
Object_width, object_height );
Gdiobj. drawimage (this. original_image, destrect, 0, 0, actual_width,
Actual_heigh, graphicsunit. pixel); IMG. save (outputfilename, system. drawing. imaging. imageformat. JPEG); forced to generate a JPEG image at a 100 PX resolution. The result is "terrible" in everyone's words. The following figure shows the image, which is about kb in size and can be seen from the local details.

2. skip the commonly used getthumbnailimage method on the Network (discussed at the end). I will first generate a higher quality format (for example, PNG and BMP, which I will generate here using PNG ), modify the last line of code: bitmap IMG = new Bitmap (object_width, object_height );
IMG. setresolution (72f, 72f );
Graphics gdiobj = graphics. fromimage (IMG );
Gdiobj. compositingquality = compositingquality. highquality;
Gdiobj. smoothingmode = smoothingmode. highquality;
Gdiobj. interpolationmode = interpolationmode. highqualitybicubic;
Gdiobj. pixeloffsetmode = pixeloffsetmode. highquality;
Gdiobj. fillrectangle (New solidbrush (color. White), 0, 0,
Object_width, object_height );
Rectangle destrect = new rectangle (0, 0,
Object_width, object_height );
Gdiobj. drawimage (this. original_image, destrect, 0, 0, actual_width,
Actual_heigh, graphicsunit. pixel); IMG. save (outputfilename, system. drawing. imaging. imageformat. PNG); image quality and. net's getthumbnailimage quality is almost the same, but the size will faint 1.54 MB, and no matter how you define the resolution, only 72px

3. It seems that PNG cannot be used. Otherwise, the loading time will be too long. Now I get the image information from the source image and export the new image to bitmap IMG = new Bitmap (object_width, object_height );
IMG. setresolution (72f, 72f );
Graphics gdiobj = graphics. fromimage (IMG );
Gdiobj. compositingquality = compositingquality. highquality;
Gdiobj. smoothingmode = smoothingmode. highquality;
Gdiobj. interpolationmode = interpolationmode. highqualitybicubic;
Gdiobj. pixeloffsetmode = pixeloffsetmode. highquality;
Gdiobj. fillrectangle (New solidbrush (color. White), 0, 0,
Object_width, object_height );
Rectangle destrect = new rectangle (0, 0,
Object_width, object_height );
Gdiobj. drawimage (this. original_image, destrect, 0, 0, actual_width,
Actual_heigh, graphicsunit. pixel); system. Drawing. imaging. encoderparameters Ep = new system. Drawing. imaging. encoderparameters (1 );
Ep. Param [0] = new system. Drawing. imaging. encoderparameter (system. Drawing. imaging. encoder. Quality, (long) 100 );
System. Drawing. imaging. imagecodecinfo ICI = getencoderinfo ("image/JPEG ");
If (ici! = NULL)
{
IMG. Save (outputfilename, ici, EP );
}
Else
{
IMG. Save (outputfilename, system. Drawing. imaging. imageformat. JPEG );
} There is almost no loss in image quality, and the image size is 568 KB. This can be considered as the best solution.

4. Finally, let's look at the method on the network. Instead of using the drawimage method to scale the image, we use the. NET thumbnail method to create (getthumbnailimage ). System. Drawing. Image thumbnailimage =
Original_image.getthumbnailimage (object_width, object_height, new system. drawing. image. getthumbnailimageabort (thumbnailcallback), intptr. zero); system. drawing. bitmap IMG = new system. drawing. bitmap (thumbnailimage); system. drawing. imaging. encoderparameters Ep = new system. drawing. imaging. encoderparameters (1 );
Ep. Param [0] = new system. Drawing. imaging. encoderparameter (system. Drawing. imaging. encoder. Quality, (long) 100 );
System. Drawing. imaging. imagecodecinfo ICI = getencoderinfo ("image/JPEG ");
If (ici! = NULL)
{
IMG. Save (outputfilename, ici, EP );
}
Else
{
IMG. Save (outputfilename, system. Drawing. imaging. imageformat. JPEG );
} The image produced is 547 K. The image quality and method 3 are almost the same, and the local colors are slightly different. However, this function has a fatal problem. If you process the image below, the result will be vomiting, and the image quality is not very bad. Where is the problem? Let's take a look at the description of the getthumbnailimage Method on msdn: If an image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the desired size. If the image does not contain an embedded thumbnail image, this method creates a thumbnail image by zooming the main image.
The problem is that if you use the EXIF tool to view the original image, you will find that a 160*120 thumbnail is included in the image. Therefore, getthumbnailimage directly returns this thumbnail, and because we have to define the size, he actually put the 160*120 thumbnail into the size I specified and returns it. We can imagine how bad the image quality is.

refer to my post on csdn: http://community.csdn.net/Expert/topic/5425/5425099.xml? Temp =. 341366 http://community.csdn.net/Expert/TopicView.asp? Id = 5400258

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.