慢图片框 [英] slow picture box

查看:65
本文介绍了慢图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a simple Ludus Latrunculorum game for my project at school and am using Picture Boxes in order to represent the pieces for the game.

但是,当我使用背景图片-我将面板放置在面板上的任何背景图片时,它绘制的速度都很慢.好像它在左上方放置了1张图片,然后等待大约0.005并放置下一张,直到木板被填满. 我尝试用1x1白色图像替换背景图像,结果相同. 但是,当我将背景设为颜色(this.board.BackColor = Color.Green;)时,它会立即打印出碎片. 此外,当我使背面颜色透明时,我将看到整个表格的原始背景,再次打印很慢. 但是,当我使用Color.Tan(它是表单的透明键)时,我看到了表单后面的内容,并且立即打印出了片段.我觉得这很奇怪,因为我猜想CPU提取表单后面的任何内容并在其上打印碎片比获取背景图像并在其上打印要困难得多.

However, when I use a background image - any background image for panel I am placing the piece at, it draws them very slowly. As if it places 1 picture top left, then waits about 0.005 and places the next one, until the board is filled. I've tried replacing the background image with a 1x1 white image, same outcome. BUT, when I make the background a color (this.board.BackColor = Color.Green;) it prints the pieces immediately. Furthermore, when I make the back color transparent, so I will see the original background of the whole form, once again, the print is very slow. But when I use Color.Tan, which is my transparency key of the form, I see whatever is behind the form, and the pieces print immediately. Which I find very odd, as I am guessing it is more difficult for the CPU to fetch whatever is behind the form and print the pieces on it than it is to fetch the background image and print on that.

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

Why is that happening? How can I make the pictures print immediately?

期望的行为-立即打印作品. 实际行为-打印件速度慢. 短代码得到同样的问题: Form1.Designer.cs

Desired behaviour - Immediate print of the pieces. Actual behaviour - Slow print of the pieces. Short code to get same problem: 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]);
                }
        }



    }
}

推荐答案

我的第一个建议是:不要使用PictureBox,因为此控件很重.如果要绘制图像,只需使用OnPaint方法绘制即可.

My first advice is: don't use PictureBox, bacuse this control is heavy. If you want to draw an image, just draw it in OnPaint method.

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

Second advice: add all the images to Resources, so you will access them much easier just by name instead of full path.

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

And one more thing: remove the background. We will draw it as well. No need to set it. So, here's my full example.

Reources.resx

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

应用

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

Note that I set DoubleBuffered flag in constructor to eliminate flickering.

这篇关于慢图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆