簡體   English   中英

C#表格繪制緩慢

[英]C# Form Draws Slowly

我在Visual Studio 2012上創建了一個應用程序,並試圖加快表格的繪制時間。

我有一個主窗體,在它的內部有一個容器,根據工具條的選擇,新的表單將顯示在其中。 它的工作原理就像一種魅力,但問題是,無論計算機的性能如何(在不同的計算機上嘗試過),繪制都需要花費大量時間,並且問題似乎是背景。

我已經為主窗體,該窗體內的容器以及我的項目中的所有窗體設置了背景圖像,因此當它們顯示時,背景圖像不會被切碎,而是繼續該圖像。 但是,如果不是使用背景作為圖片,而是讓我將背面保留為白色,那么對於主要的表單,容器和表單,它就像一個魅力。

我在互聯網上讀過有關將表單和內容中的double緩沖區設置為true的信息,但是它沒有做任何事情,花費了相同的時間。

有什么建議嗎? 提前致謝!

您可以通過手動繪制背景擠多一點的速度出來。 這有幫助,因為它允許您禁用基礎的背景色,這只會浪費時間,因為無論如何它都會被圖像覆蓋。

// Reference to manually-loaded background image
Image _bmp;

// In your constructor, set these styles to ensure that the background
// is not going to be automatically erased and filled with a color
public Form1() {
    InitializeComponent();
    SetStyle(
        ControlStyles.Opaque |
        ControlStyles.OptimizedDoubleBuffer | 
        ControlStyles.AllPaintingInWmPaint, true);

    // Load background image
    _bmp = Image.FromFile("c:\\path\\to\\background.bmp");
}

// Override OnPaint to draw the background
protected override void OnPaint(PaintEventArgs e) {
    var g = e.Graphics;
    var srcRect = new Rectangle(0, 0, _bmp.Width, _bmp.Height);
    int startY = Math.Max(0, (e.ClipRectangle.Top / _bmp.Height) * _bmp.Height);
    int startX = Math.Max(0, (e.ClipRectangle.Left / _bmp.Width) * _bmp.Width);
    for (int y = startY; y < e.ClipRectangle.Bottom; y+= _bmp.Height)
        for (int x = startX; x < e.ClipRectangle.Right; x += _bmp.Width)
        {
            var destRect = new Rectangle(x, y, _bmp.Width, _bmp.Height);
            g.DrawImage(_bmp, destRect, srcRect, GraphicsUnit.Pixel);
        }
    base.OnPaint(e);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM