This article is a mirror article of machine translation, please click here to jump to the original article.

View: 25230|Reply: 2

[Source] C# takes a specified part of an image based on the coordinates

[Copy link]
Posted on 1/19/2016 4:50:44 PM | | | |


This is a rendering, eradicate x y w h to take a screenshot of the original picture

  1. /// <summary>
  2.         /// 截取一张图片的指定部分
  3.         /// </summary>
  4.         /// <param name="bitmapPathAndName">原始图片路径名称</param>
  5.         /// <param name="width">截取图片的宽度</param>
  6.         /// <param name="height">截取图片的高度</param>
  7.         /// <param name="offsetX">开始截取图片的X坐标</param>
  8.         /// <param name="offsetY">开始截取图片的Y坐标</param>
  9.         /// <returns></returns>
  10.         public static Bitmap GetPartOfImageRec(Bitmap sourceBitmap, int width, int height, int offsetX, int offsetY)
  11.         {
  12.             //Bitmap sourceBitmap = new Bitmap(bitmapPathAndName);
  13.             Bitmap resultBitmap = new Bitmap(width, height);
  14.             using (Graphics g = Graphics.FromImage(resultBitmap))
  15.             {
  16.                 Rectangle resultRectangle = new Rectangle(0, 0, width, height);
  17.                 Rectangle sourceRectangle = new Rectangle(0 + offsetX, 0 + offsetY, width, height);
  18.                 g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
  19.             }
  20.             return resultBitmap;
  21.         }
Copy code






Previous:How about the Tenpay Enterprise Interface? Is the Tenpay interface good?
Next:Have you ever seen a 360 search that includes the inside page of the website first?
 Landlord| Posted on 1/19/2016 5:43:50 PM |
  1. public Bitmap GetPartOfImage(string bitmapPahtAndName, int width, int height, int offsetX, int offsetY)
  2.         {
  3.             Bitmap sourceBitmap = new Bitmap(bitmapPahtAndName);
  4.             Bitmap resultBitmap = new Bitmap(width, height);
  5.             for (int x = 0; x < width; x++)
  6.             {
  7.                 for (int y = 0; y < height; y++)
  8.                 {
  9.                     if (offsetX + x < sourceBitmap.Size.Width & offsetY + y < sourceBitmap.Size.Height)
  10.                     {
  11.                         resultBitmap.SetPixel(x, y, sourceBitmap.GetPixel(offsetX + x, offsetY + y));
  12.                     }
  13.                 }
  14.             }
  15.             return resultBitmap;
  16.         }
Copy code


Posted on 1/25/2016 2:53:55 PM |


Pictures zoom out and zoom in
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com