C # generate the high-definition thumbnail code. a c # function module contains comments and a function parameter. Here we can see the C # generate the thumbnail code:
01 public static void SetGoodImage (string fileName, string newFile, int maxHeight, int maxWidth, long qualitys)
02 {
03 if (qualitys = 0)
04 {
05 qualitys = 80;
06}
07 using (System. Drawing. Image img = System. Drawing. Image. FromFile (fileName ))
08 {
09 System. Drawing. Imaging. ImageFormat
10 thisFormat = img. RawFormat;
11 Size newSize = NewSize (maxWidth, maxHeight, img. Width, img. Height );
12 Bitmap outBmp = new Bitmap (newSize. Width, newSize. Height );
13 Graphics g = Graphics. FromImage (outBmp );
14 // set the painting quality of the canvas
15g. CompositingQuality = CompositingQuality. HighQuality;
16g. SmoothingMode = SmoothingMode. HighQuality;
17g. InterpolationMode = InterpolationMode. HighQualityBicubic;
18g. DrawImage (img, new Rectangle (0, 0, newSize. Width, newSize. Height ),
19 0, 0, img. Width, img. Height, GraphicsUnit. Pixel );
20g. Dispose ();
21 // The following Code sets the compression quality when saving the image
22 EncoderParameters encoderParams = new EncoderParameters ();
23 long [] quality = new long [1];
24 quality [0] = qualitys;
25 EncoderParameter encoderParam = new EncoderParameter (System. Drawing. Imaging. Encoder. Quality, quality );
26 encoderParams. Param [0] = encoderParam;
27 // obtain the ImageCodecInfo object that contains information about the built-in image decoder.
28 ImageCodecInfo [] arrayICI = ImageCodecInfo. GetImageEncoders ();
29 ImageCodecInfo javasici = null;
30 for (int x = 0;
31 x <arrayICI. Length;
32 x ++)
33 {
34 if (arrayICI [x]. FormatDescription. Equals ("JPEG "))
35 {
36 jpegICI = arrayICI [x];
37 // set JPEG encoding
38 break;
39}
40}
41 if (policici! = Null)
42 {
43 outBmp. Save (newFile, jpegICI, encoderParams );
44}
45 else
46 {
47 outBmp. Save (newFile, thisFormat );
48}
49 img. Dispose ();
50 outBmp. Dispose ();
51}
52}
Function parameter description:
View sourceprint? 1 // original file
2 // New File
3 /// maximum height
4 /// maximum width
5 // quality. If it is 0, it is set to 80.
Note: For more exciting articles, please follow the help houseProgramming TutorialTopic.