我正在尝试根据从串行端口获取的数据旋转仪表。串行通信运行良好,现在我在使仪表旋转时遇到问题。我现在正在尝试使用滑块使图像旋转,但我仍然没有运气。我目前实现了一个计时器,每 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();
}