You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.1 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing.Imaging;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ShenTun.ImageCollection.Common
  9. {
  10. public class ImageCut
  11. {
  12. #region 剪裁图片方法
  13. /// <summary>
  14. /// 剪裁 -- 用GDI+
  15. /// </summary>
  16. /// <param name="b">原始Bitmap,即需要裁剪的图片</param>
  17. /// <param name="StartX">开始坐标X</param>
  18. /// <param name="StartY">开始坐标Y</param>
  19. /// <param name="iWidth">宽度</param>
  20. /// <param name="iHeight">高度</param>
  21. /// <returns>剪裁后的Bitmap</returns>
  22. public Bitmap KiCut(Bitmap b)
  23. {
  24. if (b == null)
  25. {
  26. return null;
  27. }
  28. int w = b.Width;
  29. int h = b.Height;
  30. if (X >= w || Y >= h)
  31. {
  32. return null;
  33. }
  34. if (X + Width > w)
  35. {
  36. Width = w - X;
  37. }
  38. if (Y + Height > h)
  39. {
  40. Height = h - Y;
  41. }
  42. try
  43. {
  44. Bitmap bmpOut = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
  45. Graphics g = Graphics.FromImage(bmpOut);
  46. // Create rectangle for displaying image.
  47. Rectangle destRect = new Rectangle(0, 0, Width, Height); //所画的矩形正确,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。
  48. // Create rectangle for source image.
  49. Rectangle srcRect = new Rectangle(X, Y, Width, Height); //srcRect参数指定要绘制的 image 对象的矩形部分。 将此部分进行缩放以适合 destRect 参数所指定的矩形。
  50. g.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);
  51. //resultG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, side, side), new System.Drawing.Rectangle(0, 0, initWidth, initHeight), System.Drawing.GraphicsUnit.Pixel);
  52. g.Dispose();
  53. return bmpOut;
  54. }
  55. catch
  56. {
  57. return null;
  58. }
  59. }
  60. #endregion
  61. #region ImageCut1类的构造函数
  62. public int X;
  63. public int Y;
  64. public int Width;
  65. public int Height;
  66. /// <summary>
  67. /// ImageCut1类的构造函数,ImageCut1类用来获取鼠标在pictureBox1控件所画矩形内的图像
  68. /// </summary>
  69. /// <param name="x表示鼠标在pictureBox1控件上按下时的横坐标"></param>
  70. /// <param name="y表示鼠标在pictureBox1控件上按下时的纵坐标"></param>
  71. /// <param name="width表示鼠标在pictureBox1控件上松开鼠标的宽度"></param>
  72. /// <param name="heigth表示鼠标在pictureBox1控件上松开鼠标的高度"></param>
  73. public ImageCut(int x, int y, int width, int heigth)
  74. {
  75. X = x;
  76. Y = y;
  77. Width = width;
  78. Height = heigth;
  79. }
  80. #endregion
  81. }
  82. }