【问题标题】:.Net equivalent for ScaleHeight and Scalewidth.Net 等效于 ScaleHeight 和 Scalewidth
【发布时间】:2010-10-08 00:49:12
【问题描述】:

基本上我想做的是让我的绘画工作更轻松。

在 VB6 时代,有一个叫做 Scalewidth 和 Scaleheight 的东西,我可以将它们设置为自定义值。前任。 100.

然后,当我需要在可用空间的中心绘制一个点时,我会在 50,50 处绘制它。

.Net 中有什么方法可以让我获得类似的功能吗?

这样无论我得到多大的绘图画布,我都可以使用绝对坐标在其上绘图。

【问题讨论】:

    标签: c# .net gdi+ drawing gdi


    【解决方案1】:

    我不知道是否有办法在 .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;
    

    【讨论】:

      【解决方案2】:

      首先,您为什么不使用Graphics.ScaleTransform 而不是自己处理所有缩放?比如:

      e.Graphics.ScaleTransform( 
        100.0 / this.ClientSize.Width, 
        100.0 / this.ClientSize.Height );
      

      你的代码最终会更清晰,我敢打赌这会更快一点。

      其次,如果您坚持使用 cnvX/rcnvX 函数,请确保使用 this.ClientSize.Width(高度也是如此)而不是“this.Width”。

      【讨论】:

      • 谢谢slimCode,我今天就研究这个。可能这就是我之前一直在寻找的。​​span>
      【解决方案3】:

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-23
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 2012-01-18
        • 2011-01-29
        • 2017-03-12
        • 1970-01-01
        • 2011-03-23
        相关资源
        最近更新 更多