2010-11-17 37 views
0

我已經制作了一個程序,允許用戶在一個picturebox圖像上繪製線條,但現在需要保存這些行以便在以後打開。這是我畫的線電流代碼:保存用戶在c#中繪製位圖圖形

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
     } 
     int Drawshape; 




     private Point p1, p2; 
     List<Point> p1List = new List<Point>(); 
     List<Point> p2List = new List<Point>(); 

     private void Form1_MouseDown(object sender, MouseEventArgs e) 
     { 


     } 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Drawshape = 5; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Drawshape = 2; 
     } 

     private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (Drawshape == 5) 
      { 
       if (p1.X == 0) 
       { 
        p1.X = e.X; 
        p1.Y = e.Y; 
       } 
       else 
       { 
        p2.X = e.X; 
        p2.Y = e.Y; 

        p1List.Add(p1); 
        p2List.Add(p2); 

        pictureBox1.Invalidate(); 
        p1.X = 0; 
       } 
      } 
     } 

     private void pictureBox1_ParentChanged(object sender, EventArgs e) 
     { 

     } 

     private void pictureBox1_Paint(object sender, PaintEventArgs e) 
     { 
      Graphics G = Graphics.FromImage(pictureBox1.Image); 
      if (Drawshape == 5) 
      { 
       using (var p = new Pen(Color.Blue, 4)) 
       { 
        for (int x = 0; x < p1List.Count; x++) 
        { 
         G.DrawLine(p, p1List[x], p2List[x]); 
        } 
       } 
      } 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      pictureBox1.Invalidate(); 
     } 

     private void Save_Click(object sender, EventArgs e) 
     { 



     } 
    } 
} 

我不知道如何保存這些線條,並在當用戶想要稍後再次打開。我已經公開並保存了filedialogs,但不知道如何讓他們做我希望他們做的工作。請幫忙。

感謝

回答

2

如果你想保存顯示在圖片框的圖像,完成在運行期間可能已在其上繪製的任何行,可以使用Control.DrawToBitmap method

我無法確定您是否也在問如何使用SaveFileDialog來確定用戶想要保存文件的位置,或者您已經知道了該部分,但它非常簡單。

下面是一個完整解決方案的例子。首先,用戶通過保存對話框(標題爲「保存圖像」並默認過濾到位圖圖像(* .bmp))提示用戶。如果他們單擊確定,圖片框中顯示的圖像被繪製到一個臨時位圖,並且該臨時位圖被保存到它們指定的位置。如果他們單擊取消,該文件不會被保存,並且該方法會立即退出。

private void Save_Click(object sender, EventArgs e) 
{ 
    //Show a save dialog to allow the user to specify where to save the image file 
    using (SaveFileDialog dlgSave = new SaveFileDialog()) 
    { 
     dlgSave.Title = "Save Image"; 
     dlgSave.Filter = "Bitmap Images (*.bmp)|*.bmp|All Files (*.*)|*.*"; 
     if (dlgSave.ShowDialog(this) == DialogResult.OK) 
     { 
      //If user clicked OK, then save the image into the specified file 
      using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height)) 
      { 
       picturebox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); 
       bmp.Save(dlgSave.FileName); 
      } 
     } 
    } 
} 
0

不清楚你想要什麼...你想保存結果圖像或點的列表?

如果你想保存圖像,只需使用pictureBox1.Image.Save(fileName)

如果你想節省點列表,你可以使用序列化(它應該工作二進制或XML序列化)