2011-06-10 38 views
-1

我一直在試圖將圖像保存在pictureBox1中。試圖做的是讓人們可以通過點擊我的「另存爲」按鈕來「保存」pictureBox1中的圖像。C#將Button1保存在Pitcherbox1中.png

我使用Visual Studio 2010中使用C#

這是我到目前爲止有:

private void Button2_Click(System.Object sender, System.EventArgs e) 
    { 

     SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
     saveFileDialog1.Filter = "Png Image|*.jpg"; 
     saveFileDialog1.Title = "Save an Image File"; 
     saveFileDialog1.ShowDialog(); 
     if (saveFileDialog1.FileName != "") 
     pictureBox1.Image = new Bitmap ("c:/avatar.png"); 
     { 
      SaveFileDialog dialog = new SaveFileDialog(); 
      if (dialog.ShowDialog() == DialogResult.OK) 
      { 
       pictureBox1.Image.Save(@"C:\Documents and Settings\.Png", System.Drawing.Imaging.ImageFormat.Png); 
      } 
     } 
    } 

回答

1

我不知道你與你的代碼嘗試,似乎是一個相當那裏幾件事情不對。這是我該怎麼做的:

private void Button2_Click(System.Object sender, System.EventArgs e) 
{ 
    if (pictureBox.Image != null) 
    { 
     using {var dialog = new SaveFileDialog()) 
     { 
      dialog.Title = ... 
      saveFileDialog1.Filter = "Png Image|*.png"; 
      ...other properties... 

      if (dialog.ShowDialog == DialogResult.OK) 
      { 
       pictureBox.Image.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Png) 
      } 
     } 
    } 
}