我有一个申请显示图像内部的一个窗形式 PictureBox 控制。的 SizeMode 在控制设置 Zoom 因此,图像中所包含的 PictureBox 将显示在一个方面的正确方法不管的尺寸 PictureBox.

这是伟大的外观,因为可以大小的窗口,但是你想和图像将始终显示,使用它的最好的配合。不幸的是,我还需要处理鼠标点击事件上的图片箱和需要,以便能够翻译从屏幕上空坐标图像的空间坐标。

它看起来像是容易的转换,从屏幕上的空间控制的空间,但是我没有看到任何明显的方式翻译控制的空间图像的空间(即像素协调的来源的图像,已经缩放在图片箱)。

是有一个简单的方法来这样做,或者我应该只是重复的伸缩数学,他们在用国内的位置的图像和做翻译我自己?

有帮助吗?

解决方案

根据扩展,相对于像素可能是任何地方的一些像素。例如,如果图像是按比例缩减显着,像素2、10个可能代表2,10到20,100),所以你必须要做到的数学你自己和承担全部责任的任何错误!:-)

其他提示

我只是执行翻译。代码不是太坏,但它没有离开我希望,他们提供支持。我可以看到这样一种方法被用于很多不同的情况。

我猜这就是为什么他们增加了扩展方法:)

在代码:

// Recompute the image scaling the zoom mode uses to fit the image on screen
imageScale ::= min(pictureBox.width / image.width, pictureBox.height / image.height)

scaledWidth  ::= image.width * imageScale
scaledHeight ::= image.height * imageScale

// Compute the offset of the image to center it in the picture box
imageX ::= (pictureBox.width - scaledWidth) / 2
imageY ::= (pictureBox.height - scaledHeight) / 2

// Test the coordinate in the picture box against the image bounds
if pos.x < imageX or imageX + scaledWidth < pos.x then return null
if pos.y < imageY or imageY + scaledHeight < pos.y then return null

// Compute the normalized (0..1) coordinates in image space
u ::= (pos.x - imageX) / imageScale
v ::= (pos.y - imageY) / imageScale
return (u, v)

获得素的位置的图像,你就乘以实际图像素的尺度,但归一化的坐标让你地址原先响应者的角解决的模糊性,在个案的基础。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top