PictureBoxSizeMode.Zoom - 缩放系数是多少?
我有一个包含图像的 PictureBox(其 SizeMode 属性设置为 Zoom),两者的大小可能有所不同。
由于用户必须直接与图像交互,因此我需要能够将 PictureBox 单击坐标转换为图像坐标。
到目前为止,我所做的是将 PictureBox 的长宽比与图像的长宽比进行比较。 例如,如果 PictureBox 相对于图像而言“更宽屏”(请参见屏幕截图),则意味着 PictureBox 会将图像拉伸到其自身高度并将其水平居中,将其背景颜色(在本例中为红色)显示到屏幕上。图像的左侧和右侧。 然后我假设图像的显示高度与 PictureBox.Height 相同,并从那里开始工作。
但有一个问题。 正如您在屏幕截图(我的绿色注释)中看到的,白色图像被拉伸以适应(缩放)红色 PictureBox 的内部,在底部留下了一个小边距。
屏幕截图 http://img132.imageshack.us/img132/3541/bordernl6.th.png单击查看全尺寸。
不过,并非所有 PictureBox.Size / Image.Size 组合都会发生这种情况。 这让我相信一定有更好的方法来做到这一点。
当然,我会这么做。 我只需调整代码即可消除上下文依赖性。
但底部的边距不是来自我的代码。 它来自 PictureBox 本身。 帮我一个忙,尝试以下操作:
将 PictureBox (PictureBox1) 添加到窗体,将其停靠到窗体的所有四个侧面。 代码应如下所示:
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
运行它并调整 Form 的大小,以便拉伸(缩放)图像以填充 PictureBox 的高度,并在白色方块的左侧和右侧留下红色条。 现在,尽可能慢地垂直调整表单大小。 您应该时不时地在底部看到红色边缘。
宽度和水平调整大小也会发生同样的情况,显示右侧边距。
正是由于这个小缺陷,我知道缩放系数无法按照我一直在做的方式计算; 结果只是一个近似值。 如果尝试后您仍然认为代码是相关的,那么我很乐意发布它。
感谢您花时间阅读和回复。
I have a PictureBox (its SizeMode property set to Zoom) containing an image, and both may vary in size.
Since the user has to interact with the image directly, I need to be able to convert the PictureBox click coordinates to image coordinates.
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.
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.
screenie http://img132.imageshack.us/img132/3541/bordernl6.th.png Click for full-size.
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.
Sure, I'll do that. I just have to adapt the code to remove the context dependency.
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:
Add a PictureBox (PictureBox1) to a Form, dock it to all four sides of the Form. The code should look as follows:
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 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.
The same thing happens with the width and a horizontal resize, showing a margin to the right.
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.
Thank you for taking the time to read and respond.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有办法获得缩放系数。 我猜测由于底部的边距,您的数学可能会出现错误(也许您有一次性错误或类似性质的错误)。
您能否发布一个完整的示例来显示您正在执行的计算?
I don't think that there is a way to get the zoom factor. I'm guessing that there might be an error with your math because of the margin at the bottom (maybe you have a one-off error or something of that nature).
Could you post a complete example showing the calculations you are performing?