PictureBoxSizeMode.Zoom-缩放系数是多少? [英] PictureBoxSizeMode.Zoom - What's the zoom factor?

查看:205
本文介绍了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

运行它并调整窗体的大小,以便将图像拉伸(缩放)以填充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.

解决方案

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?

这篇关于PictureBoxSizeMode.Zoom-缩放系数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆