C # generates high-quality thumbnail programs-The ultimate algorithm _ Practical skills

Source: Internet
Author: User
First look at the code:
Using System;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.Drawing.Drawing2D;
/**////<summary>
///
* * Generate high-quality thumbnail programs * *
///
File:GenerateThumbnail.cs
///
Author: Zhou Zhenxing (Zxjay drifting away)
///
E-mail:tda7264@163.com
///
date:07-04-07
///
</summary>
public class Generatethumbnail
... {
/**////<summary>
Generating thumbnail static methods
</summary>
<param name= "Pathimagefrom" > Source map path (including filename and extension) </param>
<param name= The path saved by the "Pathimageto" > generated thumbnails (including file name and extension)
Note: The extension must be relative to the resulting thumbnail format </param>
<param name= "width" > the size of the thumbnail "canvas" (pixel value) </param>
<param name= The height of the thumbnail "canvas" (pixel value) </param> of "height" >
public static void Genthumbnail (string pathimagefrom,string pathimageto,int width,int height)
... {
Image imagefrom = null;
Try
... {
Imagefrom = Image.FromFile (Pathimagefrom);
}
Catch
... {
Throw
}
if (Imagefrom = null)
... {
Return
}
Source Chart Width and height
int imagefromwidth = Imagefrom.width;
int imagefromheight = Imagefrom.height;
The actual width and height of the resulting thumbnail
int bitmapwidth = width;
int bitmapheight = height;
The position of the resulting thumbnail on the above "canvas"
int X = 0;
int Y = 0;
Calculates the actual size of the thumbnail and its position on the canvas, based on the source diagram and the thumbnail size you want to generate.
if (Bitmapheight * imagefromwidth > Bitmapwidth * imagefromheight)
... {
Bitmapheight = Imagefromheight * width/imagefromwidth;
Y = (height-bitmapheight)/2;
}
Else
... {
Bitmapwidth = Imagefromwidth * height/imagefromheight;
X = (width-bitmapwidth)/2;
}
Create Canvas
Bitmap bmp = New Bitmap (width, height);
Graphics g = graphics.fromimage (BMP);
Empty with White
G.clear (Color.White);
Specifies a high quality dual three-time interpolation method. Perform a pre-filter to ensure high quality shrinkage. This pattern produces the highest quality conversion image.
G.interpolationmode = Interpolationmode.highqualitybicubic;
Specifies high quality, low speed rendering.
G.smoothingmode = smoothingmode.highquality;
Draws the specified portion of the specified Image at the specified location and at the specified size.
G.drawimage (Imagefrom, New Rectangle (X, Y, Bitmapwidth, Bitmapheight), new Rectangle (0, 0, Imagefromwidth, imagefromheight), GraphicsUnit.Pixel);
Try
... {
Tested. jpg format thumbnail size and quality optimization
Bmp. Save (Pathimageto, imageformat.jpeg);
}
Catch
... {
}
Finally
... {
Show Freed Resources
Imagefrom.dispose ();
Bmp. Dispose ();
G.dispose ();
}
}
}
The resulting thumbnail size is certain, no clipping, no distortion.
You can test the size and visual quality of various graphic formats, graphics quality, thumbnails generated by rendering.
Tested: Vista original default desktop. jpg format size: 1024*768,
Generate thumbnails of the original size, as compared to the following:
Original. jpg format, 223 KB
. jpg 102KB
. PNG 1816 KB
. gif 228 KB
. TIFF 2000KB above
...
Visually except for the poor quality of GIF, other than the source image of the naked eye can not distinguish (I am a little myopia ^ ^)
In the consideration of patents and versatility and other factors, the recommendation of the. jpg format.
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.