PictureBoxSizeMode.Zoom - What is the zoom factor?

I have a PictureBox (its SizeMode property set to Zoom) containing an image and both can be different in size.

Since the user needs to interact with the image directly, I need to be able to convert the clicked coordinates of the PictureBox to the coordinates of the image.

What I have done so far is comparing the aspect ratio of the PictureBox to the aspect ratio of the image. For example, if the PictureBox is "wider" in relation to the image (see screenshot), this means that the PictureBox will stretch the image to its height and center it horizontally, showing its background color (in this case red) on the left and right of Images. Then I assume the image's height is displayed the same as PictureBox.Height and work from there.

There is a problem. As you can see in the screenshot (green annotation), the white image stretched to fit (zoomed in) inside the red PictureBox leaves a small one at the bottom.

screenie http://img132.imageshack.us/img132/3541/bordernl6.th.png Click for full size.

This does not happen for all PictureBox.Size / Image.Size combinations. This makes me think there must be a better way to do this.


Of course I will. I just need to adapt the code to remove the context dependency.

But the edge at the bottom doesn't come from my code. This comes from the PictureBox itself. Do me a favor and try this:

Add a PictureBox (PictureBox1) to the form, connect it to all four sides of the form. The code should look like this:

Public Class Form1

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

        PictureBox1.BackColor = Color.Red
        PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)

        g.FillRectangle(Brushes.White, 0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height)

    End Sub

End Class

      

Run it and resize the shape so that the image is stretched (enlarged) to fill the height of the PictureBox, leaving red stripes to the left and right of the white square. Now resize the shape vertically as slowly as possible. You should see a red edge at the bottom from time to time.

The same thing happens with width and horizontal resizing showing the edge to the right.

Due to this slight imperfection that I know, the scaling factor cannot be calculated the way I did; the result will only be approximate. If, after trying this, you still find the code up to date, I will happily post it.

Thank you for taking the time to read and respond.

+1


source to share


1 answer


I don't think there is a way to get the scaling factor. I'm guessing there might be a mistake with your math because of the edge at the bottom (maybe you have a one-off error or something).



Could you post a complete example showing the calculations you performed?

0


source







All Articles