2015-02-24 54 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標誌消除閃爍。