Note that there are some explanatory texts on larger screens.

plurals
  1. POPictureBoxSizeMode.Zoom - What's the zoom factor?
    primarykey
    data
    text
    <p>I have a PictureBox (its SizeMode property set to Zoom) containing an image, and both may vary in size.</p> <p>Since the user has to interact with the image directly, I need to be able to convert the PictureBox click coordinates to image coordinates.</p> <p>What I have done so far, is compare the aspect ratio of the PictureBox to the aspect ratio of the image. For example, if the PictureBox is "more widescreen" in relation to the image (see screenshot), it means the PictureBox will stretch the image to its own height and center it horizontally, showing its background color (in this case red) to the left and right of the image. Then I assume the image's displayed height is the same as PictureBox.Height, and work from there.</p> <p>There's a problem, though. As you can see in the screenshot (green annotation mine), the white image being stretched to fit (zoomed) inside the red PictureBox leaves a small margin at the bottom.</p> <p><a href="http://img132.imageshack.us/img132/3541/bordernl6.png" rel="nofollow noreferrer">screenie http://img132.imageshack.us/img132/3541/bordernl6.th.png Click for full-size.</a></p> <p>This doesn't happen for all PictureBox.Size / Image.Size combinations, though. This leads me to believe there must be a better way to do this.</p> <hr> <p>Sure, I'll do that. I just have to adapt the code to remove the context dependency.</p> <p>But the margin at the bottom does not come from my code. It comes from the PictureBox itself. Do me a favor and try the following:</p> <p>Add a PictureBox (PictureBox1) to a Form, dock it to all four sides of the Form. The code should look as follows:</p> <pre><code>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 </code></pre> <p>Run it and resize the Form so that the image is stretched (zoomed) to fill the height of the PictureBox, leaving red bars to the left and right of the white square. Now resize the Form vertically, as slow as you can. You should see a red margin at the bottom every now and then.</p> <p>The same thing happens with the width and a horizontal resize, showing a margin to the right.</p> <p>It's because of this small imperfection that I know the zoom factor cannot be calculated the way I have been doing it; the result would only be an approximate. If after trying this you still think the code is relevant, then I'll gladly post it.</p> <p>Thank you for taking the time to read and respond.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload