Reputation: 117
I Need to move a picture in my Windows Forms application.
This works but is terribly slow. Are there any ways to move a picture faster? I want to do this because I want to reach a "Flyin' effect".
// First try
for (int i = 0; i < 500; i++)
{
//Tempbox is a picturebox
this.Tempbox.Location = new Point(this.Tempbox.Left++, 0);
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
// Second try
using (Graphics g = Graphics.FromImage(BufferBm))
{
for (int i = 0; i < 500; i++)
{
g.DrawImage(tempContolImage, new System.Drawing.Point(i, 0));
this.Tempbox.Image = BufferBm;
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
}
Upvotes: 0
Views: 136
Reputation: 3046
I would also recommend WPF because it uses directx, but if you don't have time to learn it, this can help you:
How to fix the flickering in User controls
Set DoubleBuffered = true;
Put this hack into the form code:
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
If you have usercontrol put this into it's code:
protected override CreateParams CreateParams {
get {
var parms = base.CreateParams;
parms.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN
return parms;
}
}
Upvotes: 3
Reputation: 587
Upvotes: 0
Reputation: 2072
use WPF. http://msdn.microsoft.com/de-de/library/ms752312(v=vs.110).aspx
you can also mix winForms and WPF.
If you not use WPF make sure to set doublebuffer to true
Upvotes: 2