2014-02-21 116 views
0

我需要移動我的Windows窗體應用程序中的圖片。快速移動圖片

這工作,但速度非常慢。有什麼方法可以更快地移動圖片?我想這樣做是因爲我想達到「飛翔效果」。

// 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); 
    } 
} 
+0

您需要處理的'Paint'事件,並借鑑控制,然後在一個定時器上使'Invalidate()'失效。 – SLaks

+0

嘗試在每次迭代中移動多個像素。越接近最終設置,減少像素量以使其更柔和。它在WinForms中永遠不會非常流暢。考慮使用計時器而不是循環,[DoEvents](http://stackoverflow.com/q/5181777/719186)存在問題。 – LarsTech

+0

如果您想要真正快速的繪圖,而不是使用directx,請使用GDI而不是GDI +。 –

回答

3

我也建議WPF,因爲它使用DirectX,但如果你沒有時間去學習它,這可以幫助你:

How to fix the flickering in User controls

設置DoubleBuffered = TRUE;

將這個攻入表單代碼:

protected override CreateParams CreateParams { 
    get { 
    CreateParams cp = base.CreateParams; 
    cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED 
    return cp; 
    } 
} 

如果有用戶控件將此放入它的代碼:

protected override CreateParams CreateParams { 
    get { 
    var parms = base.CreateParams; 
    parms.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN 
    return parms; 
    } 
} 
2

使用WPF。 http://msdn.microsoft.com/de-de/library/ms752312(v=vs.110).aspx

你也可以混用winForms和WPF。

如果你不使用WPF確保設置雙緩衝真

+0

感謝您的回答。不幸的是,使用Wpf並不是「真正」的可能,它是一個相對較大的Fromsapplication,它會對很多重新編碼。 Doublebuffered已設置爲true(afaik picturebox也自動雙緩衝)。 – RandomDude

0
  • 其中一個最簡單的方法可以是面板或等你般那麼did-移動窗體上的繪製圖像。
  • 另一種方法是使用轉換技術。