![](/img/trans.png)
[英]C# winforms - How to use the current mouse position as the zoom center to zoom the entire picturebox including the drawing content?
[英]How to retrieve zoom factor of a WinForms PictureBox?
我需要鼠标指针在 PictureBox 上的精确 position。
我使用 PictureBox 的 MouseMove 事件。
在这个 PictureBox 上,我使用“缩放”属性来显示图像。
在原始(未缩放)图像上获取鼠标的 position 的正确方法是什么?
有没有办法找到比例因子并使用它?
我认为需要使用 imageOriginalSize/imageShowedSize 来检索这个比例因子。
我使用这个 function:
float scaleFactorX = mypic.ClientSize.Width / mypic.Image.Size.Width;
float scaleFactorY = mypic.ClientSize.Height / mypic.Image.Size.Height;
是否可以使用此值在图像上获得 cursor 的正确 position ?
我今天必须解决同样的问题。 我希望它适用于任何宽度:高度比的图像。
这是我在原始全尺寸图像上找到'unscaled_p'点的方法。
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;
第一个技巧是确定图像相对于容器是水平还是垂直放大,因此您将知道哪个图像尺寸完全填充容器。
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'相关的未缩放图像上的点。
希望有所帮助!
如果我理解正确的话,我相信你会想做一些这样的事情:
假设:PictureBox适合图像宽度/高度,PictureBox边框与实际图像之间没有空格。
ratioX = e.X / pictureBox.ClientSize.Width;
ratioY = e.Y / pictureBox.ClientSize.Height;
imageX = image.Width * ratioX;
imageY = image.Height * ratioY;
这应该给你原始图像中的像素点。
这是一个简单的 function 来解决这个问题:
private Point RemapCursorPosOnZoomedImage(PictureBox pictureBox, int x, int y, out bool isInImage)
{
// original size of image in pixel
float imgSizeX = pictureBox.Image.Width;
float imgSizeY = pictureBox.Image.Height;
// current size of picturebox (without border)
float cSizeX = pictureBox.ClientSize.Width;
float cSizeY = pictureBox.ClientSize.Height;
// calculate scale factor for both sides
float facX = (cSizeX / imgSizeX);
float facY = (cSizeY / imgSizeY);
// use smaller one to fit picturebox zoom layout
float factor = Math.Min(facX, facY);
// calculate current size of the displayed image
float rSizeX = imgSizeX * factor;
float rSizeY = imgSizeY * factor;
// calculate offsets because image is centered
float startPosX = (cSizeX - rSizeX) / 2;
float startPosY = (cSizeY - rSizeY) / 2;
float endPosX = startPosX + rSizeX;
float endPosY = startPosY + rSizeY;
// check if cursor hovers image
isInImage = true;
if (x < startPosX || x > endPosX) isInImage = false;
if (y < startPosY || y > endPosY) isInImage = false;
// remap cursor position
float cPosX = ((float)x - startPosX) / factor;
float cPosY = ((float)y - startPosY) / factor;
// create new point with mapped coords
return new Point((int)cPosX, (int)cPosY);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.