C # Scaling and cropping Images

Source: Internet
Author: User

In GDI +, scaling and cropping can be seen as the same operation, but the selection of the original region is different. You have no choice but to look at specific algorithms for better understanding.

C # code
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4. Using System. Drawing;
  5. Using System. Drawing. Drawing2D;
  6. Using System. Drawing. Imaging;
  7. Namespace Project
  8. {
  9. Class ImageOperation
  10. {
  11. /// <Summary>
  12. /// Resize the image
  13. /// </Summary>
  14. /// <Param name = "bmp"> original Bitmap </param>
  15. /// <Param name = "newW"> New width </param>
  16. /// <Param name = "newH"> new height </param>
  17. /// <Param name = "Mode"> reserved, unavailable for the moment </param>
  18. /// <Returns> processed images </returns>
  19. Public static Bitmap ResizeImage (Bitmap bmp, int newW, int newH, int Mode)
  20. {
  21. Try
  22. {
  23. Bitmap B = new Bitmap (newW, newH );
  24. Graphics g = Graphics. FromImage (B );
  25. // Quality of interpolation algorithms
  26. G. InterpolationMode = InterpolationMode. HighQualityBicubic;
  27. G. DrawImage (bmp, new Rectangle (0, 0, newW, newH), new Rectangle (0, 0, bmp. Width, bmp. Height), GraphicsUnit. Pixel );
  28. G. Dispose ();
  29. Return B;
  30. }
  31. Catch
  32. {
  33. Return null;
  34. }
  35. }
  36. /// <Summary>
  37. /// Crop -- use GDI +
  38. /// </Summary>
  39. /// <Param name = "B"> original Bitmap </param>
  40. /// <Param name = "StartX"> Start coordinate X </param>
  41. /// <Param name = "StartY"> Start coordinate Y </param>
  42. /// <Param name = "iWidth"> width </param>
  43. /// <Param name = "iHeight"> height </param>
  44. /// <Returns> the cropped Bitmap </returns>
  45. Public static Bitmap Cut (Bitmap B, int StartX, int StartY, int iWidth, int iHeight)
  46. {
  47. If (B = null)
  48. {
  49. Return null;
  50. }
  51. Int w = B. Width;
  52. Int h = B. Height;
  53. If (StartX> = w | StartY> = h)
  54. {
  55. Return null;
  56. }
  57. If (StartX + iWidth> w)
  58. {
  59. IWidth = w-StartX;
  60. }
  61. If (StartY + iHeight> h)
  62. {
  63. IHeight = h-StartY;
  64. }
  65. Try
  66. {
  67. Bitmap bmpOut = new Bitmap (iWidth, iHeight, PixelFormat. Format24bppRgb );
  68. Graphics g = Graphics. FromImage (bmpOut );
  69. G. DrawImage (B, new Rectangle (0, 0, iWidth, iHeight), new Rectangle (StartX, StartY, iWidth, iHeight), GraphicsUnit. Pixel );
  70. G. Dispose ();
  71. Return bmpOut;
  72. }
  73. Catch
  74. {
  75. Return null;
  76. }
  77. }
  78. }
  79. }

The targets are all new Rectangle (0, 0, iWidth, iHeight). The Scaling Algorithm inserts new Rectangle (0, 0, bmp) into the target image. width, bmp. height), while cropping only inserts the new Rectangle (StartX, StartY, iWidth, iHeight) in the area with the top width and Height of the original area into the target area.

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.