我正在为我在学校的项目编写一个简单的Ludus Latrunculorum游戏,并使用图片框来表示游戏的各个部分。
但是,当我使用背景图像 – 任何背景图像的面板,我把它放在,它吸引他们很慢。 就好像在左上方放置1张图片,然后等待0.005左右放置下一张,直到板子被填满。 我已经尝试用1×1白色图像replace背景图像,结果相同。 但是,当我使背景颜色( 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]); } } } }
http://www.c-sharpcorner.com/Forums/Thread/45434/解决了这个问题。 应该启用双缓冲并将布局改为拉伸。
我的第一个建议是:不要使用PictureBox
,bacuse这个控件很重。 如果要绘制图像,只需在OnPaint
方法中绘制即可。
第二个建议:将所有的图像添加到资源,所以你只要按名称而不是完整的路径访问它们。
还有一件事:删除背景。 我们也会画出来。 无需设置它。 所以,这是我完整的例子。
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
标志以消除闪烁。