2015-02-24 55 views
0

我正在为我在学校的项目编写一个简单的Ludus Latrunculorum游戏,并且正在使用图片框来表示游戏片断。慢图片框

但是,当我使用背景图像 - 任何背景图像作为面板时,我将它放在上面,这会非常缓慢地吸引它们。就好像它在左上方放置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]); 
       } 
     } 



    } 
} 
+0

发布您的代码有问题。 – 2015-02-24 06:21:42

+0

_ [寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含**期望的行为**,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处](http:// stackoverflow。com/help/on-topic)_ – MickyD 2015-02-24 06:25:20

+0

@CoderofCode 谢谢您的回复! http://pastebin.com/KtDKEMHz – user2227218 2015-02-24 06:26:25

回答

0

我的第一个建议是:不要用PictureBox,bacuse这个控件很重。如果您想绘制图像,只需在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标志消除闪烁。