C # image Cropping

Source: Internet
Author: User

 

[C-sharp]View plaincopy

  1. /// <Summary>
  2. /// Narrow down the cropped Image
  3. /// </Summary>
  4. /// <Param name = "int_width"> to narrow down the cropped image width </param>
  5. /// <Param name = "int_height"> reduce the length of the cropped image </param>
  6. /// <Param name = "input_imgurl"> path of the image to be processed </param>
  7. /// <Param name = "out_imgurl"> processed image path </param>
  8. Public void imgreducecutout (INT int_width, int int_height, string input_imgurl, string out_imgurl)
  9. {
  10. // === Size of the uploaded standard image ===
  11. Int int_standard_width = 160;
  12. Int int_standard_height = 160;
  13. Int reduce_width = 0; // The reduced width.
  14. Int reduce_height = 0; // The reduced height.
  15. Int cutout_width = 0; // specifies the cropped width.
  16. Int cutout_height = 0; // crop height
  17. Int level = 100; // the range of the thumbnail quality from 1
  18. // === Get zoom out, crop size ===
  19. If (int_standard_height * int_width/int_standard_width> int_height)
  20. {
  21. Performance_width = int_width;
  22. Performance_height = int_standard_height * int_width/int_standard_width;
  23. Cutout_width = int_width;
  24. Cutout_height = int_height;
  25. }
  26. Else if (int_standard_height * int_width/int_standard_width <int_height)
  27. {
  28. Performance_width = int_standard_width * int_height/int_standard_height;
  29. Performance_height = int_height;
  30. Cutout_width = int_width;
  31. Cutout_height = int_height;
  32. }
  33. Else
  34. {
  35. Performance_width = int_width;
  36. Performance_height = int_height;
  37. Cutout_width = int_width;
  38. Cutout_height = int_height;
  39. }
  40. // === Create an image object through a connection ===
  41. System. Drawing. Image oldimage = system. Drawing. image. fromfile (server. mappath (input_imgurl ));
  42. // === Zoom out image ====
  43. System. Drawing. Image thumbnailimage = oldimage. getthumbnailimage (performance_width, performance_height, new system. Drawing. image. getthumbnailimageabort (thumbnailcallback), intptr. Zero );
  44. Bitmap Bm = new Bitmap (thumbnailimage );
  45. // === Functions for processing JPG quality ====
  46. Imagecodecinfo [] codecs = imagecodecinfo. getimageencoders ();
  47. Imagecodecinfo ICI = NULL;
  48. Foreach (imagecodecinfo Codec in codecs)
  49. {
  50. If (codec. mimetype = "image/JPEG ")
  51. ICI = codec;
  52. }
  53. Encoderparameters Ep = new encoderparameters ();
  54. Ep. Param [0] = new encoderparameter (encoder. Quality, (long) level );
  55. // Bm. Save (server. mappath ("2.jpg"), ici, EP );
  56. // === Crop an image ====
  57. Rectangle clonerect = new rectangle (0, 0, cutout_width, cutout_height );
  58. Pixelformat format = BM. pixelformat;
  59. Bitmap clonebitmap = BM. Clone (clonerect, format );
  60. // === Save the image ====
  61. Clonebitmap. Save (server. mappath (out_imgurl), ici, EP );
  62. }

 

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.