问题
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