Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hellow
i am trying to anamate text its size is 200 ,it works but i notice a flicker in the letters.I tryed by drawstring using 2 diffrent colors
(white to erase string then Red to draw the string ),i also did the similar thing using a image jpg but no diffrence
any suggestion or if you know any other method
C#
Timer tm = new Timer();
Image imageFile;
PointF po;
Graphics g;
//---------------------------
SolidBrush brushRed = new System.Drawing.SolidBrush(Color.FromArgb(255, 0, 0));
SolidBrush brushWhite = new System.Drawing.SolidBrush(Color.FromArgb(255, 255, 255));
Font font1 = new System.Drawing.Font("Tahoma", 200F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
float xx , yy = 10;
Graphics g;
Rectangle rect;
//--------------------------------
public Form1()
{
    InitializeComponent();
}

void f1(Object x,EventArgs e)
{
    g.DrawString("Jan-2-2015", font1, brushWhite, xx, yy);
    xx = xx + 5;
    g.DrawString("Jan-2-2015", font1, brushRed, xx, yy);
    if ((int)xx >= panel1.Width) xx = -panel1.Width;

    /*rect = new Rectangle((int)xx, (int)yy, imageFile.Width, imageFile.Height);
    g.FillRectangle(brushWhite, rect);
    xx = xx + 1;
    po = new PointF(xx, 10.0F);
    g.DrawImage(imageFile, po);
    if ((int)xx >= pictureBox1.Width) xx = -imageFile.Width;
    */
}

private void Timer1_Click(object sender, EventArgs e)
{
    tm.Interval = 20;
    tm.Enabled = true;
    tm.Tick += new EventHandler(f1);
    tm.Start();
}

private void Stop_Timer1(object sender, EventArgs e)
{
    tm.Stop();
    g.Dispose();
}

private void FormLoad(object sender, EventArgs e)
{
   g=panel1.CreateGraphics();
   xx = -panel1.Width;
   rect = new Rectangle(1, 1, panel1.Width, panel1.Height);
}
Posted
Updated 3-Jan-15 0:08am
v3

1 solution

First off, do the drawing in the Panel.Paint event and use the supplied Graphics object instead of creating your own Graphics context each time. You then Invalidate the panel in the Timer Tick event handler.

That won't cure your problem, but it will neaten up your code and make it work better...
Then set the Panel.DoubleBuffered property[^] to true and the flicker should be gone.
 
Share this answer
 
Comments
Engineer khalid 3-Jan-15 6:39am    
1- i did the doubleBuffer
2- i wrote Invalidate(); in void f1(Object x,EventArgs e)//see my code above
3- i declare g2 globally
System.Drawing.Graphics g2;
in OnPaint i wrote
g2.DrawString("Jan-2-2015", font1, brushWhite, xx, yy);
xx = xx + 5;
g2.DrawString("Jan-2-2015", font1, brushRed, xx, yy);
if ((int)xx >= panel1.Width) xx = -panel1.Width;
the result is a red cross appeared on the Form
OriginalGriff 3-Jan-15 7:49am    
Stop creating your own graphics contexts!
Look at the OnPaint method signature: the parameter is a PaintEventArgs, which means it contains the graphics context you need.
Engineer khalid 3-Jan-15 7:32am    
it works
those are the changes

in FormLoad
SetDoubleBuffered(this);
in OnPaint
e.Graphics.DrawString("Happy new year", font1, brushWhite, xx, yy);
xx = xx + 5;
e.Graphics.DrawString("Happy new year", font1, brushRed, xx, yy);
if ((int)xx >= this.ClientSize.Width) xx = -this.ClientSize.Width;
in f1 i wrote
void f1(Object x,EventArgs e)
{
if(flage==1)Invalidate();//flage is global initially=0;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900