0

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);
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
RandomDude
  • 109
  • 2
  • 9
  • You need to handle the `Paint` event and draw on the control, then `Invalidate()` on a timer. – SLaks Feb 21 '14 at 16:05
  • Try moving by more than a single pixel on each iteration. The closer it gets to the final setting, decrease the pixel amount to give it a softer ending. It will never be super smooth in WinForms. Consider using a timer instead of your loop, the [DoEvents](http://stackoverflow.com/q/5181777/719186) is problematic. – LarsTech Feb 21 '14 at 16:23
  • If you want real fast drawings and not using directx use GDI instead of GDI+. – γηράσκω δ' αεί πολλά διδασκόμε Feb 21 '14 at 17:00
  • to extends @valter ´s suggestion: take a look at *CachedBitmap*. Afaik it uses GDI (without +). http://msdn.microsoft.com/en-us/library/ms533835%28v=vs.85%29.aspx – BudBrot Feb 21 '14 at 20:40

3 Answers3

3

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;
  }
}
Community
  • 1
  • 1
speti43
  • 2,886
  • 1
  • 20
  • 23
2

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

Mat
  • 1,960
  • 5
  • 25
  • 38
  • Thanks for your answer. Unfortunatly it is not "really" possible to use Wpf, it is a relatively huge Fromsapplication, it would be to much recoding. Doublebuffered is set already to true (afaik picturebox is also automatically doublebuffered). – RandomDude Feb 21 '14 at 16:12
0
  • One of the easiest way can be drawing the image on a panel or etc -like you did- then move it on the form.
  • Another way can be using transformation techniques.