1

I would need place a specific part of an Image in a PictureBox, for example a 32x32 image at the location 0,0 in the image.
It would look like this:
actorsample.png Note the red border is not in the Image, just pasted it in for clarity.

Question:
How would I do this, but that i could still use the Image in a picturebox?

Ian H.
  • 3,840
  • 4
  • 30
  • 60
  • 1
    Possible duplicate of [How to crop an image using C#?](http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c) What do you mean by "still using" the image? You can only assign one `Image` to a `PictureBox`. You need to save the original away / keep a reference to it somewhere else if you want to "still use it". – Maximilian Gerhardt Dec 03 '15 at 18:13
  • @MaximilianGerhardt with **use** I mean that I can move it around by modifiying it location and not locking it to the form for example. – Ian H. Dec 03 '15 at 18:58
  • Please further specify "moving it around" and "not locking it to the form". You could always keep a copy of the original picture, then on some input you crop the part of the image you want (depended on the location / the `Rect` used in cropping it) and display it in the PictureBox. Or do you mean to drag around the image in the PictureBox on mouse input? – Maximilian Gerhardt Dec 03 '15 at 19:03

1 Answers1

4

It has been a while, but one easy trick is to put the PictureBox inside a container element that is 32x32 and move the PictureBox around inside the container to change which image you actually want displayed. Setting the (top,left) of PictureBox to (0,0) would show image 1, and setting it to (0,-32) would should image 2, (0,-64)=>3, (-32,0)=>4, etc.

ASCII ART of idea

+-+
|o|oo
+-+oo
 oooo
 oooo

Image of empty container:

+-+
| |
+-+
Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • Thanks for the downvote with no reason. Apparently you don't like the idea, or perhaps you think that copying image data around (possibly from main memory to video memory) might somehow be faster than just issuing a recrop without needing to retransfer the image data? Sure there are trade-offs (extra window handle, entire image loaded into object -- but for a single object that isn't repeated you'd need a copy anyhow), but useful in MANY cases. And almost perfect use case if you assume the graphic is of a single player that updates as it moves (which is what it looks like the OP wants) – Robert McKee Dec 03 '15 at 18:27
  • This is precisely what I need for my application. You got my up vote. There are many ways to solve most programming problems. This solution is not the best for every application, but it IS the best for many... and probably the easiest in many ways. In this case, you let C# do the work instead of adding extra code and having to remember to dispose of unused bitmaps. – Mark T Oct 06 '21 at 09:04