我正在尝试在GraphicsPath上实现动态缩放,其中用户绘制矩形以定义缩放区域(请参阅下面代码中的selectRect)。当用户释放鼠标时,GraphicsPath被缩放并(通过矩阵)转换到正确的位置,以显示由' drawArea'定义的矩形中的缩放区域。
目的是让用户动态放大很多次。
以下用于计算比例和平移值的代码在用户第一次缩放时非常有效。但是,任何后续缩放都不会产生预期的结果。
if (e.Button == MouseButtons.Left)
{
PointF p = ConvertScreenToWorld(new PointF(selectRect.Width, selectRect.Height));
PointF p2 = ConvertScreenToWorld(new PointF(selectRect.X, selectRect.Y));
tmpScale = new PointF(drawArea.Width / Math.Max(1, p.X) , drawArea.Height / Math.Max(1, p.Y));
tmpTranslate = new PointF(-p2.X * tmpScale.X, -p2.Y * tmpScale.Y);
this.Invalidate();
}
ConvertScreenToWorld()
private PointF ConvertScreenToWorld(PointF PointIn)
{
PointF[] p = new PointF[] { PointIn };
Matrix m = new Matrix();
m.Translate(tmpTranslate.X, tmpTranslate.Y);
m.Scale(tmpScale .X, tmpScale .Y);
m.Invert();
m.TransformPoints(p);
return p[0];
}
绘图代码: (注意意图是保留原始路径数据,这就是我使用克隆的原因)
Matrix m2 = new Matrix();
m2.Translate(tmpTranslate.X, tmpTranslate.Y);
m2.Scale(tmpScale.X, tmpScale.Y);
GraphicsPath gpClone2;
gpClone2 = (GraphicsPath)gp.Clone();
gpClone2.Transform(m2);
g.DrawPath(Pens.Blue, gpClone2);
有关如何使用累积缩放功能的任何建议都将非常感激。