如何检索一个WinForms图片框的缩放因子? [英] How to retrieve zoom factor of a WinForms PictureBox?

查看:240
本文介绍了如何检索一个WinForms图片框的缩放因子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的鼠标指针在一个图片框的精确位置。

I need the precise position of my mouse pointer over a PictureBox.

我用的图片框的MouseMove事件。

I use the MouseMove event of the PictureBox.

在这个图片框,我使用了放大属性来显示图像。

On this PictureBox, I use the "zoom" property to show an image.

什么是获得对鼠标的位置,正确的方法原始(未缩放)图像?

What is the correct way for getting the position of the mouse on the original (unzoomed) image?

有没有办法找到比例因子和使用它吗?

Is there a way to find the scale factor and use it?

我,认为需要使用imageOriginalSize / imageShowedSize检索这个比例系数

I think need to use imageOriginalSize/imageShowedSize to retrieve this scale factor.

我使用此功能:

float scaleFactorX = mypic.ClientSize.Width / mypic.Image.Size.Width;
float scaleFactorY = mypic.ClientSize.Height / mypic.Image.Size.Height;



时可以使用这个值来得到光标移到图像的正确位置?

Is possible to use this value to get the correct position of the cursor over the image?

推荐答案

我今天不得不解决这个相同的问题。我想它的任何宽度的图像工作:高度比率。

I had to solve this same problem today. I wanted it to work for images of any width:height ratio.

下面是我的方法发现,原来全尺寸图片上的点unscaled_p。

Here's my method to find the point 'unscaled_p' on the original full-sized image.

            Point p = pictureBox1.PointToClient(Cursor.Position);
            Point unscaled_p = new Point();

            // image and container dimensions
            int w_i = pictureBox1.Image.Width; 
            int h_i = pictureBox1.Image.Height;
            int w_c = pictureBox1.Width;
            int h_c = pictureBox1.Height;



第一招是,以确定该图像是水平或垂直更大相对于容器,所以你会知道哪些图像尺寸完全填充容器

The first trick is to determine if the image is a horizontally or vertically larger relative to the container, so you'll know which image dimension fills the container completely.

            float imageRatio = w_i / (float)h_i; // image W:H ratio
            float containerRatio = w_c / (float)h_c; // container W:H ratio

            if (imageRatio >= containerRatio)
            {
                // horizontal image
                float scaleFactor = w_c / (float)w_i;
                float scaledHeight = h_i * scaleFactor;
                // calculate gap between top of container and top of image
                float filler = Math.Abs(h_c - scaledHeight) / 2;  
                unscaled_p.X = (int)(p.X / scaleFactor);
                unscaled_p.Y = (int)((p.Y - filler) / scaleFactor);
            }
            else
            {
                // vertical image
                float scaleFactor = h_c / (float)h_i;
                float scaledWidth = w_i * scaleFactor;
                float filler = Math.Abs(w_c - scaledWidth) / 2;
                unscaled_p.X = (int)((p.X - filler) / scaleFactor);
                unscaled_p.Y = (int)(p.Y / scaleFactor);
            }

            return unscaled_p;

请注意,由于变焦中心图像时,'填充剂'长度必须在因素来确定维不是由图像填补。结果,unscaled_p',是未缩放的图像是P关联到的点。

Note that because Zoom centers the image, the 'filler' length has to be factored in to determine the dimension that is not filled by the image. The result, 'unscaled_p', is the point on the unscaled image that 'p' correlates to.

希望帮助!

这篇关于如何检索一个WinForms图片框的缩放因子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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