2014-03-26 39 views
0

我創建一個繪畫程序,我已經找到了如何繪製形狀的形式,但我怎麼救他們,我試着這樣做:如何保存繪製形狀,圖像C#

Image bmp = new Bitmap(this.Width - panel1.Width, this.Height - panel1.Height); 
using (Graphics g = Graphics.FromImage(bmp)) 
{ 

} 
bmp.Save("test.bmp"); 

它可以保存test.bmp,但文件是空的?

繼承人是我的全部代碼:

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 Paint_Program 
{ 

public partial class PaintImg : Form 
{ 



    public Boolean veryimportantbool = false; 
    int drawcount = 0; 
    int brushsize = 16; 

    public PaintImg() 
    { 
     InitializeComponent(); 
     //Cursor drawCursor = new Cursor("Pencil.cur"); 
     //this.Cursor = drawCursor; 


    } 


    private void Form1_Load(object sender, EventArgs e) 
    { 


     //Cursor.Position = new Point(this.Location.X - this.Width/2, this.Location.Y - this.Height/2) 
     UserControl1 userc1 = new UserControl1(); 
     userc1.Show(); 
     brushsize = Convert.ToInt16(label1.Text); 


    } 

    private void rectangleShape1_Click(object sender, EventArgs e) 
    { 

    } 

    private void Test_Click(object sender, EventArgs e) 
    { 
     //testing 
     //Console.WriteLine("X:" + mousex); 
     //onsole.WriteLine("Y: " + mousey); 
    } 

    private void Form1_SizeChanged(object sender, EventArgs e) 
    { 

    } 


    public void Form1_MouseClick(object sender, MouseEventArgs i) 
    { 

      drawcount = drawcount + 1; 
      System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red); 
      System.Drawing.Graphics formGraphics; 
      formGraphics = this.CreateGraphics(); 
      formGraphics.FillRectangle(myBrush, new Rectangle(i.X, i.Y, brushsize, brushsize)); 
      myBrush.Dispose(); 
      formGraphics.Dispose(); 
      count.Text = Convert.ToString(drawcount); 


    } 

    private void newToolStripButton_Click(object sender, EventArgs e) 
    { 
     //SetupDoc newsetup = new SetupDoc(); 
     //newsetup.ShowDialog(); 
    } 

    private void PaintImg_MouseMove(object sender, MouseEventArgs e) 
    { 

    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 

    } 

    private void panel1_Click(object sender, MouseEventArgs e) 
    { 
     drawcount = drawcount + 1; 
     System.Drawing.SolidBrush myBrush = new  System.Drawing.SolidBrush(System.Drawing.Color.Red); 
     System.Drawing.Graphics formGraphics; 
     formGraphics = this.CreateGraphics(); 
     formGraphics.FillRectangle(myBrush, new Rectangle(e.X, e.Y, brushsize, brushsize)); 
     myBrush.Dispose(); 
     formGraphics.Dispose(); 
     count.Text = Convert.ToString(drawcount); 
    } 

    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     brushsize = brushsize + 1; 
     label1.Text = Convert.ToString(brushsize); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     brushsize = brushsize - 1; 
     label1.Text = Convert.ToString(brushsize); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     Image bmp = new Bitmap(this.Width - panel1.Width, this.Height - panel1.Height); 
     using (Graphics g = Graphics.FromImage(bmp)) 
     { 

     } 
     bmp.Save("test.bmp"); 
    } 

} 
} 

如果有人新的如何做到這一點,我會appreicate它???? :D

+0

你想捕捉面板作爲位圖並保存它? –

+0

我想捕獲窗體上的所有繪製圖形,並將它們保存到一個.bmp文件 – user3349095

回答

0

您發佈的代碼沒有任何問題 - 它完全繪製了您編碼的內容 - 什麼都沒有。它會創建一個漂亮的空文件,其大小已經指定(根據您傳遞的參數,這可能是錯誤的)。

得到的東西出現在你需要繪製位圖 - 即重構代碼panel1_Click畫,或至少硬編碼的東西:

Image bmp = new Bitmap(20, 20); 
using (Graphics g = Graphics.FromImage(bmp)) 
{ 
    g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(3, 3, 8, 8)); 
} 
bmp.Save("test.bmp"); 
+0

好吧,但我如何保存形式上已經繪製的形狀 – user3349095

+0

@ user3349095您是否在尋找「屏幕截圖」?通常您只需再次繪製相同的圖片/序列,而不是試圖捕獲之前您的代碼繪製的內容... –

+0

繪製形狀的位置由用戶單擊表單的位置決定,因此如何記錄用戶單擊的每個位置? – user3349095

1
Bitmap bmp; 
Graphics objGraphics; 
Rectangle rt; 
Point pnt; 

rt = this.ClientRectangle; 
pnt = this.PointToScreen(new Point(0, 0)); 

bmp = new Bitmap(rt.Width, rt.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 

objGraphics = Graphics.FromImage(bmp); 
objGraphics.CopyFromScreen(pnt.X, pnt.Y, 0, 0, rt.Size, CopyPixelOperation.SourceCopy); 
objGraphics.Dispose(); 

bmp.Save("test.bmp"); 

bmp.Dispose(); 

瓦爾特

+0

使用時應該添加保存文件格式,如果不是,則文件不能打開.. \t \t \t bmp.Save(「test.bmp」,ImageFormat.Bmp); – aswzen