I have to draw a moving Arrow image (using C# code VS2008) between 2 Bars (image). I tried it with two approaches:
- Draw arrow figure by joining points. and then keep changing the x,y coordinates.
- making an arrow image and then keep changing its position.
The issue is flickering. Whenever I move the arrow image (or redraw the points) nearby images (actually there few other images as Bars are there) flickers. I tried to call invalidate(rect) thinking it will redraw the given rectangle only but still as soon the arrow image crosses these bars, everything flickers. which doesn't look good. how can I move this arrow image in a smooth fashion. Pls. suggest. Or should I adopt another approach?
// Code: **
public partial class Form1 : Form { private Bitmap bitmapAntenna; private Bitmap bitmapArrow;
public Form1()
{
bitmapAntenna = new Bitmap("D:\\AntennaBar.JPG");
bitmapArrow = new Bitmap("D:\\Arrow.JPG");
}
private void DrawAntennaImages(int antennaNo)
{
Graphics grph = this.CreateGraphics();
if (1 == antennaNo)
{
grph.DrawImage(bitmapAntenna, new RectangleF(350, 300, bitmapAntenna.Width*(1.00F), bitmapAntenna.Height*(1.00F))) ;
}
else
grph.DrawImage(AntennaImage, new Point(550, 300));
}
private void DrawArrowImages(int X)
{
Graphics grph = this.CreateGraphics();
grph.DrawImage(bitmapArrow, new Point(380 + X, 350));
}
private void PaintHandler(Object sender, System.Windows.Forms.PaintEventArgs e)
{
DrawAntennaImages(1);
DrawAntennaImages(2);
}
private void button_Click(object sender, EventArgs e)
{
for (int i = 1; i < 40; i++)
{
// Note: first draw antenna 1 image, arrow image then antenna 2 image so that arrow will overlap
// only antenna 1 image only.
DrawAntennaImages(1); // Draw Antenn 1 image
DrawArrowImages(i * 2); // Draw Arrow image
DrawAntennaImages(2); // Draw Antenn 2 image
System.Threading.Thread.Sleep(25); // Sleep before drawing next time ..
}
this.Invalidate(); // If I don't call this, arrow image does not elope after last draw
}
}
// In designer
this.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintHandler);
//Code *
Thanks RPS