C# 在pictureBox中绘制旋转线
我想在C# 在pictureBox中绘制旋转线,c#,picturebox,flicker,C#,Picturebox,Flicker,我想在PictureBox中绘制一条线的旋转动画。我使用pictureBox1.CreateGraphics()来画线,但我听说这种方法不适合PictureBox。另外,我在PictureBox窗口上遇到了严重的闪烁现象,有什么建议吗?以下是代码段: private void OnTimedEvent(object source, PaintEventArgs e) { try { if (comport.BytesToRe
PictureBox
中绘制一条线的旋转动画。我使用pictureBox1.CreateGraphics()
来画线,但我听说这种方法不适合PictureBox
。另外,我在PictureBox
窗口上遇到了严重的闪烁现象,有什么建议吗?以下是代码段:
private void OnTimedEvent(object source, PaintEventArgs e)
{
try
{
if (comport.BytesToRead > 0)
{
X = comport.ReadByte();
Y = comport.ReadByte();
Z = comport.ReadByte();
}
Graphics g = pictureBox1.CreateGraphics();
Pen red = new Pen(Color.Red, 2);
g.TranslateTransform(100, 80 - X);
g.RotateTransform(120);
g.DrawLine(red, new Point(100, 0 + X), new Point(100, 80 - X));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
timer1.Interval = 1;
pictureBox1.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
尝试在pictureBox1.Paint事件处理程序中移动绘图代码,并在需要重新绘制pictureBox1时调用pictureBox1.Invalidate。这才是真正的画法。此时,您正在闪烁,因为您每秒钟都会重新绘制一幅图片,但此时您没有要绘制的基本体
byte X;
byte Y;
byte Z;
private void OnTimedEvent(object source, PaintEventArgs e)
{
try
{
if (comport.BytesToRead > 0)
{
X = comport.ReadByte();
Y = comport.ReadByte();
Z = comport.ReadByte();
}
pictureBox1.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
timer1.Interval = 1;
pictureBox1.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen red = new Pen(Color.Red, 2);
g.TranslateTransform(100, 80 - X);
g.RotateTransform(120);
g.DrawLine(red, new Point(100, 0 + X), new Point(100, 80 - X));
}
尝试在pictureBox1.Paint事件处理程序中移动绘图代码,并在需要重新绘制pictureBox1时调用pictureBox1.Invalidate。这才是真正的画法。此时,您正在闪烁,因为您每秒钟都会重新绘制一幅图片,但此时您没有要绘制的基本体
byte X;
byte Y;
byte Z;
private void OnTimedEvent(object source, PaintEventArgs e)
{
try
{
if (comport.BytesToRead > 0)
{
X = comport.ReadByte();
Y = comport.ReadByte();
Z = comport.ReadByte();
}
pictureBox1.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
timer1.Interval = 1;
pictureBox1.Invalidate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen red = new Pen(Color.Red, 2);
g.TranslateTransform(100, 80 - X);
g.RotateTransform(120);
g.DrawLine(red, new Point(100, 0 + X), new Point(100, 80 - X));
}
还有一件事,如果我想在pictureBox中画一条线而不清除现有的线,我应该怎么做?有两种可能的方法。如果你想画有限数量的线,你可以将X的每个新值存储到一个列表或数组中(我更喜欢列表,因为它们可以更容易地增长),当有足够的值(比如10-20)时,你应该添加新值并删除最旧的值。然后在每个绘制事件中,您都应该迭代这些值并绘制线。另一种方法是在位图上绘制每一条新线,而不清除旧线,但最终位图将被线重载,图片将没有任何意义。如果你想要更多的澄清,我可以。还有一件事,如果我想在图片框中画一条线而不清除现有的线,我应该怎么做?有两种可能的方法。如果你想画有限数量的线,你可以将X的每个新值存储到一个列表或数组中(我更喜欢列表,因为它们可以更容易地增长),当有足够的值(比如10-20)时,你应该添加新值并删除最旧的值。然后在每个绘制事件中,您都应该迭代这些值并绘制线。另一种方法是在位图上绘制每一条新线,而不清除旧线,但最终位图将被线重载,图片将没有任何意义。如果你需要更多的澄清,我可以。