0

我正在尝试根据从串行端口获取的数据旋转仪表。串行通信运行良好,现在我在使仪表旋转时遇到问题。我现在正在尝试使用滑块使图像旋转,但我仍然没有运气。我目前实现了一个计时器,每 100 毫秒触发一次并运行此代码。但是,当我移动 sliderBar 时,屏幕上的图像没有任何反应。我使用计时器的原因是因为我将在最终实现中使用它。使用计时器而不是串行事件来触发 UI 更新使应用程序运行更加流畅。

一如既往,非常感谢任何帮助!

在构造函数中...

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

然后在计时器事件中......

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

    }
4

1 回答 1

2

关键问题是:

  • 实际上没有画出你产生的路径

  • 不旋转您添加到路径的多边形(添加必须应用变换)

这里有很多内存泄漏的可能性 - 需要释放具有非托管组件的对象(此处的 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();
}

我认为这应该可行,如果仍然有问题,请在此处发表评论,如果需要,我将对其进行修改。

(编辑:看起来有很多我没想到的东西要处理)

于 2013-01-04T21:25:33.957 回答