【发布时间】:2010-10-08 00:49:12
【问题描述】:
基本上我想做的是让我的绘画工作更轻松。
在 VB6 时代,有一个叫做 Scalewidth 和 Scaleheight 的东西,我可以将它们设置为自定义值。前任。 100.
然后,当我需要在可用空间的中心绘制一个点时,我会在 50,50 处绘制它。
.Net 中有什么方法可以让我获得类似的功能吗?
这样无论我得到多大的绘图画布,我都可以使用绝对坐标在其上绘图。
【问题讨论】:
基本上我想做的是让我的绘画工作更轻松。
在 VB6 时代,有一个叫做 Scalewidth 和 Scaleheight 的东西,我可以将它们设置为自定义值。前任。 100.
然后,当我需要在可用空间的中心绘制一个点时,我会在 50,50 处绘制它。
.Net 中有什么方法可以让我获得类似的功能吗?
这样无论我得到多大的绘图画布,我都可以使用绝对坐标在其上绘图。
【问题讨论】:
我不知道是否有办法在 .NET 中实现这一点,但您可以自己轻松实现:
// Unscaled coordinates = x, y; canvas size = w, h;
// Scaled coordinates = sx, sy; Scalewidth, Scaleheight = sw, sh;
x = (sx / sw) * w;
y = (sy / sh) * h;
// Or the other way round
sx = (x / w) * sw;
sy = (y / h) * sh;
【讨论】:
首先,您为什么不使用Graphics.ScaleTransform 而不是自己处理所有缩放?比如:
e.Graphics.ScaleTransform(
100.0 / this.ClientSize.Width,
100.0 / this.ClientSize.Height );
你的代码最终会更清晰,我敢打赌这会更快一点。
其次,如果您坚持使用 cnvX/rcnvX 函数,请确保使用 this.ClientSize.Width(高度也是如此)而不是“this.Width”。
【讨论】:
Schnaader 的想法是正确的……最终我实现了四个函数来做到这一点。功能如下
private float cnvX(double x)
{
return (float)((Width / 100.00) * x);
}
private float rcnvX(double x)
{
return (float)(x / Width) * 100;
}
private float rcnvY(double y)
{
return (float)((y / Height) * 100);
}
private float cnvY(double y)
{
return (float)((Height / 100.00) * y);
}
【讨论】: