我正致力于使我的应用程序从串行端口读取数据并更新UI上的标尺更高效,并且我想要询问有关处理UI更改的代码的一些建议。我有一个计时器设置为检查发送到COM端口的数据,另一个计时器使用从COM端口收到的变量更新UI。基本上发生了什么是我旋转仪表。这里是我处理图形的代码...使用定时器更新C#中的用户界面
void timer_Tick(object sender, EventArgs e) //Timer regulates how often the gauge is updated on the UI
{
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((int)_xCor, (int)_yCor);
Image img = new Bitmap(400, 400); // The box tht contains the image <--- Play around with this more
pictureBox1.Image = img; // Setting the img Image to the pictureBox class?
Graphics g = Graphics.FromImage(pictureBox1.Image); // G represents a drawing surface
Matrix mm1 = new Matrix();
//
mm1.RotateAt((float)(90 + (((12.5 * state) - 20.95) * 6)), new Point((int)_xrotate, (int)_yrotate), MatrixOrder.Append);
GraphicsPath gp = new GraphicsPath();
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();// prevent possible memory leaks
gp.Dispose();// prevent possible memory leaks
g.Dispose(); // prevent possible memory leaks
pictureBox1.Refresh();
}
我想知道是否有更有效的方法,我可以在屏幕上旋转图像。我觉得必须有,但我无法弄清楚。
你能具体谈谈您希望它是 “更有效”?例如,你觉得定时器回调在没有必要时被调用(自上次回调以来没有改变),方法是否太慢,使用的内存太多?你看到一些可衡量的性能问题吗? – dgvid
这更多的是性能问题,当我尝试在我的笔记本电脑上运行应用程序时,响应非常缓慢。例如,对传感器的响应应该在1-3秒后出现。 – Bubo
标题是关于使用计时器更新UI的问题,但您实际上担心图形效率?你有很多无关的细节,只是浑水。 –