【问题标题】:Gauge Needle Pivot Point量规针枢轴点
【发布时间】:2014-07-13 10:21:37
【问题描述】:

我的数学不好。我已经在互联网上搜索过,但无法弄清楚。

我很难保持速度计类型的仪表指针定位。

量规是半圈或 180 度。针是一个宽 10 像素、高 164 像素的 png 图像。

仪表宽 378 像素,高 226 像素。

因为我正在尝试旋转一个高矩形,所以我必须重新计算针的位置。

Point pivotPoint = new Point(gauge.Width / 2, gauge.Height); 
double degrees = -45; // example where to point the needle

谁能给我一些实际的 C# 代码行来保持针 png 正确定位?

非常感谢您的帮助。

【问题讨论】:

  • 这是另一个想法 -> 将针的图像(具有透明背景)展开为正方形并旋转它。这样您就不必重新计算位置
  • 它已经在透明背景和矩形上 - 我已经尝试过正方形但仍然没有帮助。谢谢。
  • 我的意思是你可以扩大图像,这样针就可以作为一个半径。这样,当您旋转它时,中心将固定在您想要的位置
  • 我想我现在明白你的意思了 - 如果我不能让它在数学上工作,我会使用一个大盒子容器。我不知道为什么有人投了反对票。我还有其他才能,只是在努力学习。再次感谢。

标签: c# winforms


【解决方案1】:

这里有一些代码可以将旋转的针图像绘制到带有仪表图像的 PictureBox 上。

我从从 0 到 180 的轨迹栏获取角度。

针头倒立:

  • 这个角度弥补了我们开始的 90° 偏移。
  • 针的旋转点应与其原点一致。

PictureBox.SizeMode 设置为 Autosize。

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (Bitmap needle =  new Bitmap("D:\\needle3.png" )  )
    {
        int angle = trBar_angle.Value;
        label1.Text = angle.ToString("###0°");

        // this is the offset after the translation
        Point targetPoint = new Point(-needle.Width / 2, needle.Width / 2);

        // shortcuts for the sizes
        Size nSize = needle.Size;
        Size pSize = pictureBox1.ClientSize;

        // this is the translation of the graphic to meet the rotation point
        int transX = pSize.Width / 2;
        int transY = pSize.Height - nSize.Width / 2;

        //set the rotation point and rotate
        e.Graphics.TranslateTransform( transX, transY );
        e.Graphics.RotateTransform(angle + 90); 

        // draw on the rotated graphics
        e.Graphics.DrawImage(needle, targetPoint);

        //reset everything
        e.Graphics.RotateTransform( -angle - 90); 
        e.Graphics.TranslateTransform( -transX, -transY );

    }

}

我的针: 结果:

我希望你有一个比我做的更好的量规;-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2016-06-07
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多