2011-04-16 41 views
0

首先,我很抱歉我的英語不好。現在我遇到了C#項目(ms paint)的問題。當我在圖片框中打開新圖片時,我繪製的最後一個圖形仍然保留,直到我在該圖像上繪製另一條線。這裏是我的代碼:
-draw行:創建新圖片框時出現問題!

public Form1() 
    { 
     InitializeComponent(); 
     snapshot = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
    } 
if (tempDraw != null) 
       { 
        tempDraw = (Bitmap)snapshot.Clone(); 
        Graphics g = Graphics.FromImage(tempDraw); 
        Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5); 
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
        myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; 
        g.DrawLine(myPen, pDau, pHientai); 
        myPen.Dispose(); 
        e.Graphics.DrawImageUnscaled(tempDraw, 0, 0); 
        g.Dispose(); 
       } 

- 鼠標事件:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
     paint = false; 
     snapshot = (Bitmap)tempDraw.Clone(); 

    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     paint = true; 
     saved = false; 
     pDau = e.Location; 
     tempDraw = (Bitmap)snapshot.Clone(); 
    } 
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (paint) 
     { 
      pHientai = e.Location; 
      pictureBox1.Invalidate(); 
      saved = false; 
     } 
    } 

- 創造新的圖片框:

public void New() 
    { 

     pictureBox1.Image =null; 
     snapshot = null; 
     tempDraw = null; 
     snapshot= new Bitmap(pictureBox1.Width, pictureBox1.Height); 
    } 

- 開放的形象:

New(); 
       snapshot = new Bitmap(openFileDialog1.FileName); 
       tempDraw = (Bitmap)snapshot.Clone(); 
       pictureBox1.Image = new Bitmap(openFileDialog1.FileName); 
       strPath = openFileDialog1.FileName; 
       this.Text = strPath + " - Paint"; 

你能告訴我什麼問題嗎?非常感謝你!

回答

0

在您的第一個代碼示例中,我假設整個if語句實際上都在表單的Paint事件之內嗎?就像這樣:

private void Form_Paint(object sender, PaintEventArgs e) 
{ 
    if (tempDraw != null) 
    { 
     tempDraw = (Bitmap)snapshot.Clone(); 
     Graphics g = Graphics.FromImage(tempDraw); 
     Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5); 
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
     myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; 
     g.DrawLine(myPen, pDau, pHientai); 
     myPen.Dispose(); 
     e.Graphics.DrawImageUnscaled(tempDraw, 0, 0); 
     g.Dispose(); 
    } 
} 

如果是這樣,考慮調用e.Graphics.Clear(this.BackColor)以清除表單有自己的背景顏色。這將有效地清除你繪製的任何東西。另外,如果任何方法拋出異常,則在創建繪圖對象時要考慮using聲明以保護您。我會重寫您的if聲明,如下所示:

if (tempDraw != null) 
    { 
     tempDraw = (Bitmap)snapshot.Clone(); 

     using (Graphics g = Graphics.FromImage(tempDraw)) 
     using (Pen myPen = new Pen(colorPickerDropDown1.SelectedColor, 5)) 
     { 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
      myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; 
      g.DrawLine(myPen, pDau, pHientai); 
      e.Graphics.Clear(this.BackColor); // clear any drawing on the form 
      e.Graphics.DrawImageUnscaled(tempDraw, 0, 0); 
     } 
    } 
+0

首先,感謝您的回覆。但是這個'if'語句實際上在picturebox_paint事件中。我在Form1上添加了一個圖片框。然後你的代碼只是改變我的圖片框顏色相同的形式的背景。我的問題是當我在picturebox上繪製了一些東西,然後從計算機上打開了新的圖像,我繪製的最後一個圖形沒有消失,直到我在picturebox上繪製了其他圖形,它消失了。 – Superquay 2011-04-16 17:58:18

+0

'PictureBox'不適用於繪圖。它是爲了顯示圖像,通常來自文件。直接在你的「表格」或「面板」上繪製。 – 2011-04-16 18:08:02