Search code examples
c#imagelocationpictureboxpartial

how to insert part of image into picturebox?


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...)


Solution

  • 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 :)