【发布时间】:2013-12-01 21:34:29
【问题描述】:
我试图弄清楚如何将鼠标位置(屏幕坐标)转换为在 direct2d 表面上绘制的底层转换图像上的对应点。 这里的代码应该被认为是伪代码,因为我使用了一个修改过的 c++/CLI 包装器,用于 c# 的 direct2d,除了我自己的项目之外,你将无法在任何东西中编译它。
Render()
{
//The transform matrix combines a rotation, followed by a scaling then a translation
renderTarget.Transform = _rotate * _scale * _translate;
RectF imageBounds = new RectF(0, 0, _imageSize.Width, _imageSize.Height);
renderTarget.DrawBitmap(this._image, imageBounds, 1, BitmapInterpolationMode.Linear);
}
Zoom(float zoomfactor, PointF mousepos)
{
//mousePos is in screen coordinates. I need to convert it to image coordinates.
Matrix3x2 t = _translate.Invert();
Matrix3x2 s = _scale.Invert();
Matrix3x2 r = _rotate.Invert();
PointF center = (t * s * r).TransformPoint(mousePos);
_scale = Matrix3x2.Scale(zoomfactor, zoomfactor, center);
}
这是不正确的,当缩放因子平滑增加或减少时,缩放中心开始疯狂移动,即使鼠标指针在客户端表面的中心不动,生成的缩放功能也不平滑并且闪烁很多。我尝试了所有我能想到但无法弄清楚的组合。
如果我将缩放中心点设置为 (imagewidth/2, imageheight/2),则生成的缩放是平滑的,但始终以图像中心为中心,所以我很确定闪烁不是由于其他原因程序的错误部分。
谢谢。
【问题讨论】:
-
尝试将
t * s * r替换为r * s * t。并确保屏幕点位于客户端坐标中,并考虑到 (可能是自定义的) DPI。 -
我已经尝试过了,闪烁更严重,mousepos的东西只是为了更好地说明我的目标;只要我没有可靠的解决方案,我就将其固定在客户中心进行测试。