-1

I'm currently working on an application, to automate the generation of tilesets. Actually my question is fairly simple. I'm having trouble with seperating the file into tiles. Is it possible to create seperate images out of a PictureBox or is there another, more effective way? I want to cut the graphic into tiles, to rearrange them.

LW001
  • 2,452
  • 6
  • 27
  • 36
  • 2
    your code and any errors? – Jaquarh Feb 11 '16 at 16:51
  • 2
    Thank you for responding, Well I'm actually only looking for an idea tbh. Handling it with pictureBoxes just doesn't feel right. – Derpington Steve Feb 11 '16 at 16:54
  • Of course, there are many ways. I would: create them and stuff them into an ImageList, provided they fit the 256x256 limit. Otherwise a List will do just as well. Then drawImage them onto, maybe a picturebox or maybe into a picturebox.image whenever you want to chage the layout.. To create the tiles use a suitable drawimage overload and a graphics object created from the target bitmap. – TaW Feb 11 '16 at 17:00
  • 1
    The only problem that was left was to actually get the content tile per tile of the loaded image. But, welll.. just noticed that there is actually a CroppedBitmap class.. This will make things way easier, thank you for your time. – Derpington Steve Feb 11 '16 at 17:08
  • Well, good luck with your project; it sounds fun. Note that CroppedBitmap is not part of winforms, though. – TaW Feb 11 '16 at 17:11

1 Answers1

1

You can get a sub-image from a PictureBox relatively easily is the image is just a bitmap. You can use the bitmap classes Clone() method, which takes a Rectangle and PixelFormat.

Bitmap image = pictureBox.Image;
Bitmap subImage = image.Clone(new Rect(0,0,64,64), image.PixelFormat);

The subimage in this case would start at position (0,0) in the image and by 64x64 in size

In order to rearrange your tiles you can print them back onto the PictureBox like so:

Graphics g = Graphics.FromImage(image);
g.drawImage(subImage, 64, 64);
pictureBox.Image = image;

This will draw subImage into the image at (64,64) we grabbed from the picturebox, image, earlier and then set the PictureBox image to the edited one.

Keiran Young
  • 145
  • 5
  • 1
    This may well work fine. Do however [read this](http://stackoverflow.com/questions/12709360/whats-the-difference-between-bitmap-clone-and-new-bitmapbitmap) to understand the consequences of using clone, i.e. of making only a shallow copy – TaW Feb 11 '16 at 17:09
  • Thanks, I didn't realise that was how clone worked. Though in this case only the newly defined image would be affected correct? – Keiran Young Feb 11 '16 at 17:21
  • Yes, or rather __all__ those tiles he cuts from the original will depend on it. – TaW Feb 11 '16 at 17:32