C#表格绘制缓慢

大堂

我在Visual Studio 2012上创建了一个应用程序,并试图加快表格的绘制时间。

我有一个主窗体,在它的内部有一个容器,根据工具条的选择,新的表单将显示在其中。它的工作原理就像一种魅力,但问题是,无论计算机的性能如何(在不同的计算机上尝试过),绘制都需要花费大量时间,并且问题似乎是背景。

我已经为主要表单,该表单内的容器以及我的项目中的所有表单设置了背景图像,因此当它们显示时,背景图像不会被切碎,而是继续该图像。但是,如果不是使用背景作为图片,而是让我将背面保留为白色,那么对于所有主要的表单,容器和表单,它就像是一种魅力。

我在互联网上读过有关在表单和东西中设置双缓冲区为true的信息,但它没有做任何事情,花费了相同的时间。

有什么建议吗?提前致谢!

罗杰·N

你可以挤一点通过手动绘制背景更快的速度出来。这很有帮助,因为它允许您禁用基础的背景色,这只会浪费时间,因为无论如何它都会被图像覆盖。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章