1

I'd have a program that will allow you to draw lines over an image which will eventually be used for calculating distance.

To make things simple, my current image (which is in a PictureBox) is an image of a ruler. When you left click, a path is created and drawn.

Originally, to zoom in, I had it so that a new bitmap would be created with the images new size and I was able to use Graphics.ScaleTransform and it worked fine but it would just crop the image.

I needed the image to actually change width and height so now what I'm doing is just adding/subtracting a constant zoom amount to the width & height when zooming in/out.

With this approach, I can't seem to scale the graphics and the paths are skewed into different directions and not the right size when the image is zoomed in. I completely understand why this is happening, because the image is getting larger and the graphics are staying the same, I just need whatever math is required to scale the graphics.

I've tried using Graphics.ScaleTransform as well as moving the graphics x & y to their current position + the current zoom amount (offset)

AcePilot10
  • 178
  • 1
  • 12
  • 1
    You do realize that the picturebox can zoom all by itself? You can then get the factor and use scaletransform when drawing.. note that you need to either resize the pbox to the aspect ratio of the image or allow for the left or top gap. [Examples](https://stackoverflow.com/search?q=user%3A3152130+picturebox+zoom+scale) – TaW Jul 31 '19 at 10:45
  • 1
    As TaW mentioned, use the `Zoom` mode of the PictureBox. Some other methods to draw on scaled imges, no matter the size of the canvas: [Translate Rectangle Position in Zoom Mode Picturebox](https://stackoverflow.com/q/53800328/7444103) -- [How to crop a section of an Image at cursor position?](https://stackoverflow.com/a/56128394/7444103) – Jimi Jul 31 '19 at 11:03
  • 1
    About the ruler: if you haven't alrady rolled your own, see this CodeProject: [Ruler Control](https://www.codeproject.com/Articles/4369/Ruler-Control). – Jimi Jul 31 '19 at 11:11
  • 1
    @TaW Ah, see, that probably would have been very helpful if I knew that before... Will give this a shot. Thanks! – AcePilot10 Jul 31 '19 at 11:20
  • @TaW This works! Could you change it to an asnwer so I can mark it? – AcePilot10 Jul 31 '19 at 11:36
  • Glad to hear that. But I don't even know what you did.. - You may want to write an answer yourself, though. I'll look at it and if necessary improve on it with a suggestion or two.. – TaW Jul 31 '19 at 11:47

1 Answers1

0

As directed by @TaW I changed the zooming functionality to calculate a new Width & Height based on the whatever zoom was applied then create a new Bitmap which contained the original image with the new width and height.

AcePilot10
  • 178
  • 1
  • 12
  • Hm, that doesn't sound as what I had in mind. I thought to calculated a new PictureBox.Size with pbox.SiteMode=Zoom. Nest the PBox in an AutoScrolling Panel and you have scrolling plust zooming all for almost free.. – TaW Jul 31 '19 at 12:41