慢画框

时间:2015-02-24 06:21:12

标签: c# windows winforms performance picturebox

我正在为学校的项目编写一个简单的Ludus Latrunculorum游戏,并使用Picture Box来代表游戏的各个部分。

然而,当我使用背景图像 - 面板的任何背景图像时,我将它放在那里,它会非常缓慢地绘制它们。好像它在左上方放置了1张图片,然后等待大约0.005并放置下一张图片,直到电路板被填满。 我尝试用1x1白色图像替换背景图像,结果相同。 但是,当我为背景设置一种颜色(this.board.BackColor = Color.Green;)时,它会立即打印出碎片。 此外,当我使背面颜色透明时,我会看到整个表格的原始背景,再一次,打印速度很慢。 但是当我使用Color.Tan(这是表单的透明度键)时,我会看到表单背后的内容,并立即打印出来。我发现这很奇怪,因为我猜测CPU更难以获取表单背后的内容并在其上打印碎片而不是获取背景图像并在其上打印。

为什么会这样?如何立即打印图片?

所需行为 - 立即打印件。 实际行为 - 打印件缓慢打印。 获得相同问题的简短代码: 的 Form1.Designer.cs

using System.Drawing;

namespace WindowsFormsApplication6
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackgroundImage = (Image)Image.FromFile(@"C:\Users\gold\Documents\Visual Studio 2013\Projects\Ludus Latrunculorum\Ludus Latrunculorum\images\Background.png", true); // Comment that and see how it prints the pictures immediately
            this.ClientSize = new System.Drawing.Size(907, 595);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion
    }
}

Form1.cs的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            PrintBoard();
        }

        private PictureBox[,] piecesImg;

        private void PrintBoard()
        {

            this.piecesImg = new PictureBox[8, 8];

            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                {
                    this.piecesImg[i, j] = new PictureBox();
                    this.piecesImg[i, j].ClientSize = new Size(52, 52);
                    this.piecesImg[i, j].Image = (Image)Image.FromFile(@"C:\Users\gold\Documents\Visual Studio 2013\Projects\Ludus Latrunculorum\Ludus Latrunculorum\images\White Pawn.png", true);
                    this.piecesImg[i, j].BackColor = Color.Transparent;
                    this.piecesImg[i, j].Location = new Point(j * 57, i * 57);
                    this.Controls.Add(this.piecesImg[i, j]);
                }
        }



    }
}

2 个答案:

答案 0 :(得分:2)

我的第一个建议是:不要使用PictureBox,否则这个控件很重。如果您想绘制图像,只需使用OnPaint方法绘制它。

第二条建议:将所有图像添加到资源中,这样您就可以通过名称而不是完整路径更轻松地访问它们。

还有一件事:删除背景。我们也将绘制它。无需设置它。所以,这是我的完整例子。

<强> Reources.resx resources

<强> Form1.cs的

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DoubleBuffered = true;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.DrawImage(Properties.Resources.Background, 0, 0);
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                e.Graphics.DrawImage(Properties.Resources.Pawn, new Rectangle(j * 57, i * 57, 52, 52));
    }
}

申请
screenshot

请注意,我在构造函数中设置DoubleBuffered标志以消除闪烁。

答案 1 :(得分:1)

http://www.c-sharpcorner.com/Forums/Thread/45434/解决了这个问题。 应该启用双缓冲并将布局更改为拉伸。

相关问题