how to insert part of image into picturebox?

徘徊边缘 提交于 2019-12-07 22:32:38

问题


I'm not really sure if it is possible to insert a part of image into picturebox, but I would like to create an image 500*500 pixels in size and then use the parts of it as small connectable 50*50 pieces by setting the location of image inside the pictureboxes...

Is anything similar possible through use of graphics? I'm not very familiar with it... (I am talking about C# forms application...)


回答1:


After some time of searching and few personal attempts I have found a solution, this isn't my own, but sadly I have forgot where did I took it from:

   private static Image cropImage(Image img, Rectangle cropArea)
   {
       Bitmap bmpImage = new Bitmap(img);
       Bitmap bmpCrop = bmpImage.Clone(cropArea,
       bmpImage.PixelFormat);
       return (Image)(bmpCrop);
   }

This will created cropped image, you can now use it in code. SAMPLE:

   Picturebox P = new Picturebox;
   P.BackgroundImage = cropImage(ImageThatWillBeCropped, new Rectangle(0,0,50,50));

If anyone finds this useful and needs explanation for rectangle, please, feel free to ask :)




回答2:


Thank you for your good answer. It is better to p.Image:

P.Image = cropImage(ImageThatBeCropped, new Rectangle(0, 0, 50,50));

To see image in real size.



来源:https://stackoverflow.com/questions/15512392/how-to-insert-part-of-image-into-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!