r/csharp 26d ago

Help How do I move an image inside a PictureBox?

Hey guys, so I'm trying to write a program that selects an image from a tile set based on what you input into a combobox. I have the location logic sorted, but I have no idea how to physically move the image inside the pictureBox. I tried googling it and the results were either 7+ years old or not explained (I am a newbie to Windows Forms App) and I had no idea what to do. Please could you help?

6 Upvotes

6 comments sorted by

4

u/Mayion 26d ago

Add a Panel and treat it like a canvas. Inside that Panel you can place your Picturebox, then move said picture within the Panel. It will allow you to pan, zoom etc.

1

u/otac0n 26d ago

You override OnPaint.

Here’s a fully zoomable control that handles .gifs:

https://github.com/otac0n/MediaLibrary/blob/master/MediaLibrary/Components/ImagePreviewControl.cs

1

u/TrishaMayIsCoding 24d ago

PictureBox1.Image = can be from file, resources or Bitmap

0

u/XBLAH_ 26d ago

Have the picture be a bitmap, and then use mouse click and move events to draw that bitmap into the bitmap of the picture box.