C # generating high-definition thumbnails based on large images

Source: Internet
Author: User

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.

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.