2013-05-09 45 views
2

我正在試圖製作一個簡單的應用程序,用戶可以在面板上繪製並將其保存到計算機中作爲位圖。然而,當我進入保存部分時,我得到的只是一個空白(白色)位圖。在面板上繪圖,另存爲位圖

我一直在瀏覽很多其他解決方案,我非常確定我正在保存位圖,所以我開始懷疑我的繪圖過程是否不正確。這到底是什麼錯誤?

public partial class Form1 : Form 
{ 

    SolidBrush brush; 
    Pen pen; 
    Point[] points = new Point[3]; 
    Graphics display; 
    Bitmap bmap; 

    public Form1() 
    { 
     InitializeComponent(); 
     display = panel1.CreateGraphics(); 
     bmap = new Bitmap(panel1.Width, panel1.Height); 
    } 


    private void panel1_MouseDown(object sender, MouseEventArgs e) 
    { 
     brush = new SolidBrush(Color.Black); 
     pen = new Pen(Color.Black); 


     display.FillEllipse(brush, e.X, e.Y, 10, 10); 
     panel1.DrawToBitmap(bmap, new Rectangle(0, 0, panel1.Width, panel1.Height)); 

     //this.Invalidate(); 
    } 



    private void clearToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     Graphics display = panel1.CreateGraphics(); 
     display.Clear(panel1.BackColor); 

    } 

    private void saveToolStripMenuItem_Click(object sender, EventArgs e) 
    { 



     bmap.Save(@"C:\Temp\Test.bmp"); 

    } 
} 

編輯 有了這個版本,我只看到一個黑色BMP,我甚至不看到正在創建了我的屏幕上省略號。雖然我注意到如果我將無效和繪製位圖返回到mousedown事件中,那麼保存按鈕將保存最後一個橢圓,而屏幕上仍然沒有任何東西出現。

private void panel1_MouseDown(object sender, MouseEventArgs e) 
     { 

      mousedown = true; 
      x = e.X; 
      y = e.Y; 

     } 

     private void panel1_Paint(object sender, PaintEventArgs e) 
     { 
      //Graphics g = e.Graphics; 
      if(mousedown==true) 

      { 
      brush = new SolidBrush(Color.Black); 
      pen = new Pen(Color.Black); 

      Graphics.FromImage(bmap).FillEllipse(brush, x, y, 10, 10); 
      panel1.Invalidate(); 
      //panel1.DrawToBitmap(bmap, new Rectangle(0, 0, panel1.Width, panel1.Height)); 


      //panel1.Invalidate(); 

      } 
     } 
+0

關於CreateGraphics有很多很多問題。始終以相同的答案,不要使用它。相反,使用Graphics.FromImage()繪製位圖。 panel1.Invalidate()獲取它的屏幕更新,panel1.Paint事件來繪製它。 – 2013-05-09 11:25:32

+0

我在這裏有點迷路。我使用panel1_MouseDown只是爲了保持鼠標點擊的座標以及鼠標點擊的布爾值。現在我甚至無法再看到我的橢圓,相反,面板保持白色,而保存的文件完全是黑色的。我將編輯我的更新版本的帖子。 – krikara 2013-05-09 11:48:16

回答

3

正如漢斯做了大部分在他的評論的工作,這裏是如何你的代碼也許應該看看:

public partial class Form1 : Form { 
    Bitmap bmap; 

    public Form1() { 
    InitializeComponent(); 

    bmap = new Bitmap(panel1.ClientWidth, panel1.ClientHeight); 
    panel1.MouseDown += panel1_MouseDown; 
    panel1.Paint += panel1_Paint; 
    } 

    void panel1_Paint(object sender, PaintEventArgs e) { 
    e.Graphics.DrawImage(bmap, Point.Empty); 
    } 

    void panel1_MouseDown(object sender, MouseEventArgs e) { 
    using (Graphics g = Graphics.FromImage(bmap)) { 
     g.FillEllipse(Brushes.Black, e.X, e.Y, 10, 10); 
    } 
    panel1.Invalidate(); 
    } 

    private void clearToolStripMenuItem_Click(object sender, EventArgs e) { 
    using (Graphics g = Graphics.FromImage(bmap)) { 
     g.Clear(Color.White); 
    } 
    panel1.Invalidate(); 
    } 

    private void saveToolStripMenuItem_Click(object sender, EventArgs e) { 
    bmap.Save(@"c:\temp\bmap.bmp"); 
    } 
} 

的createGraphics只是一個暫時的畫布,所以你很少,如果有的話,請使用用於繪圖的目的,特別是因爲您正在嘗試保存圖像。

0

這將工作正常。我測試了它並且運行良好

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace drawing 
{ 
    public partial class Form2 : Form 
    { 
     Graphics g; 
     bool startPaint = false; 
     int? initX = null; 
     int? initY = null; 

     bool drawSquare = false; 
     bool drawRectangle = false; 
     bool drawCircle = false; 
     public Form2() 
     { 
      InitializeComponent(); 

      bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height); 

     } 
     Bitmap bmp; 

     protected override void OnLoad(EventArgs e) 
     { 
      base.OnLoad(e); 

     } 
     void panel1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (startPaint) 
      { 
       using (g = Graphics.FromImage(bmp)) 
       { 
        // g.FillEllipse(Brushes.Black, new Rectangle(e.X, e.Y , 5, 5)); 

        Pen p = new Pen(btn_PenColor.BackColor, float.Parse(cmb_PenSize.Text)); 
        g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y)); 
        initX = e.X; 
        initY = e.Y; 
        //g.DrawImage(bmp, new Rectangle(e.X - 4, e.Y - 4, 8, 8)); 
       } 
       panel1.Invalidate(); 
      } 
     } 
     private void pnl_Draw_MouseDown(object sender, MouseEventArgs e) 
     { 
      startPaint = true; 
      if (drawSquare) 
      { 
       //Use Solid Brush for filling the graphic shapes 
       SolidBrush sb = new SolidBrush(btn_PenColor.BackColor); 
       //setting the width and height same for creating square. 
       //Getting the width and Heigt value from Textbox(txt_ShapeSize) 
       g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text)); 
       //setting startPaint and drawSquare value to false for creating one graphic on one click. 
       startPaint = false; 
       drawSquare = false; 
      } 
      if (drawRectangle) 
      { 
       SolidBrush sb = new SolidBrush(btn_PenColor.BackColor); 
       //setting the width twice of the height 
       g.FillRectangle(sb, e.X, e.Y, 2 * int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text)); 
       startPaint = false; 
       drawRectangle = false; 
      } 
      if (drawCircle) 
      { 
       SolidBrush sb = new SolidBrush(btn_PenColor.BackColor); 
       g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text)); 
       startPaint = false; 
       drawCircle = false; 
      } 
     } 
     private void pnl_Draw_MouseUp(object sender, MouseEventArgs e) 
     { 
      startPaint = false; 
      initX = null; 
      initY = null; 
     } 
     void panel1_Paint(object sender, PaintEventArgs e) 
     { 
      e.Graphics.DrawImage(bmp, Point.Empty); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      bmp.Save("D://filename.jpg", ImageFormat.Png); 
     } 


    } 
} 
+0

當我使用此代碼時,面板會閃爍,當我嘗試繪製時 – aswzen 2016-09-05 03:33:18