Issue with rotating picturebox images in winforms

First post on stackoverflow, be gentle.

I recently started messing around with pygame so I thought I’d use winforms to rig up a world creator. I have a bunch of “Tile” UserControls that are just a picturebox. They store their location and rotation independent of one another (supposedly), however when I use the MouseDown event on one tile to change it’s rotation it seems to affect all other tiles with the same image. Even stranger, is that tiles with different images don’t seem to be affected. I output the position and rotation data to a json file which holds the correct rotations for the tiles that were clicked on, but visually the tiles appear rotated in the winforms app. Is this some bug with winforms and the way it caches images? or am I doing something wrong? The whole project is up at https://github.com/L00GIE/Tile-Mapper

In the gif you can see me right-click the top-left dirt tile, then I let go of the mouse button, but when I hover over any other tiles with the same image they also rotate (but the rotation value of the objects themselves only changes for the clicked tile according to the outputted json data)
Tile Issue

I’ve tried creating a new Image and replacing the picturebox image altogether (that is the latest iteration on github), however this ends in the same result.

  • 1

    It seems you assigned the same image instance to all of your picture boxes. Once you rotate the image, it gets rotated everywhere, it’s just the controls weren’t invalidated and repainted until you hovered them. Either use a different image instance for the rotated images, or forget PictureBox and just subscribe the Paint event of the background Form or Panel. You can use TranslateTransform and RotateTransform before each Graphics.DrawImage call to rotate the canvas before drawing on it.

    – 

  • This fixed it! Thank you.

    – 

As György pointed out I was assigning the same instance of the image to each Tile and they were only getting updated on invalidation.
setting the image to a new bitmap copy of the intended image fixed it.

Leave a Comment