使用矩阵.c#中的RotateAt

本文关键字:中的 RotateAt | 更新日期: 2023-09-27 17:50:39

我正试图根据我从串行端口获取的数据旋转仪表。串行通信工作良好,现在我有问题,使压力表旋转。我现在试着用滑动条旋转图像,我仍然没有运气。我目前有一个计时器实现触发每100毫秒,并运行此代码。然而,当我移动滑动条时,屏幕上的图像没有任何变化。我使用计时器的原因是因为这是我将在我的最终实现中使用的。使用计时器来触发UI更新,而不是使用Serial事件,可以使应用程序运行得更加流畅。

如往常一样,任何帮助都是非常感谢的!

 public Form1()
    {
        InitializeComponent();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        imgpic = (Image)pictureBoxBase.Image.Clone(); // This is storing an image in a picture box...
        foreach (int rate in baudRates)
        {
            brbox.Items.Add(rate);
        }
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = 100;
        timer.Enabled = true;
        timer.Start();
        com.DataReceived += new SerialDataReceivedEventHandler(OnReceived);
    }

那么在timer事件中…

   void timer_Tick(object sender, EventArgs e) // Again it is initially drawing the picture, but it does not rotate with the statusBar
    {
        Point test = new Point(0, 0);
        Image img = new Bitmap(400, 400); 
        pictureBox1.Image = img;
        Graphics g = Graphics.FromImage(pictureBox1.Image);
        Matrix mm1 = new Matrix();
        mm1.RotateAt((trackBar1.Value),new Point( 0,0),MatrixOrder.Append);
        GraphicsPath gp = new GraphicsPath();
        gp.Transform(mm1);
        gp.AddPolygon(new Point[] { new Point(0, 0), new Point(imgpic.Width, 0), new Point(0, imgpic.Height) });
        PointF[] pts = gp.PathPoints;
        g.DrawImage(imgpic, test);
        pictureBox1.Refresh();
    }

使用矩阵.c#中的RotateAt

关键问题是:

  • 没有实际绘制生成的路径

  • 不旋转你添加到路径的多边形(必须在添加它们后应用transform )

这里有很多潜在的内存泄漏-具有非托管组件的对象(这里的Graphics, GraphicsPath, Image和Matrix对象)需要被处理,以便底层Windows对象可以很好地删除()。. NET不能为你做这些)。

修复代码:

void timer_Tick(object sender, EventArgs e)
{
    if (pictureBox1.Image != null)
        pictureBox1.Image.Dispose(); // dispose old image (you might consider reusing it rather than making a new one each frame)
    Point test = new Point(0, 0);
    Image img = new Bitmap(400, 400); 
    pictureBox1.Image = img;
    Graphics g = Graphics.FromImage(pictureBox1.Image);
    Matrix mm1 = new Matrix();
    mm1.RotateAt((trackBar1.Value), new Point( 0,0), MatrixOrder.Append); // note that the angle is in degrees, so make sure the trackbar value or input is suitably scaled
    GraphicsPath gp = new GraphicsPath();
    gp.AddPolygon(new Point[] { new Point(0, 0), new Point(imgpic.Width, 0), new Point(0, imgpic.Height) });
    //PointF[] pts = gp.PathPoints; // not needed for this task
    g.DrawPath(Pens.Black, gp); // draw the path with a simple black pen
    g.Transform = mm1; // transform the graphics object so the image is rotated
    g.DrawImage(imgpic, test); // if the image needs to be behind the path, draw it beforehand
    mm1.Dispose();
    gp.Dispose();
    g.Disose(); // prevent possible memory leaks
    pictureBox1.Refresh();
}

我认为这应该可以工作,如果它仍然有问题,在这里评论,如果需要我会修改它。

(编辑:看起来有很多东西要处理,我没有完全期望)