通过对话框将PictureBox图像写入光盘

时间:2014-02-02 16:09:52

标签: c# picturebox savefiledialog

我想知道是否有人可以帮我解决问题。

我是编程新手,希望制作自己的照片浏览器。我认为它在大多数情况下都很有效。

我的问题是我希望能够将我打开的图像保存为新图像。我有一些按钮来更改图片。我的问题是我有一个保存对话框,当完成定义保存,然后按保存,它只是重新打开一个新的保存对话框而不写入光盘。我的问题是我怎样才能实际写入文件并防止每次我尝试使用对话框保存时显示保存?

这是我的代码:

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
   //Displays a SaveFile Dialog so the user can save the image
    //assigned to SaveAs tool strip
    SaveFileDialog savefiledialog1 = new SaveFileDialog();
    savefiledialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|Png Image|*.png";
    savefiledialog1.Title = "Save an Image File";
    saveFileDialog1.ShowDialog();

    //if the file name is not an empty string open it for saving.
    if (savefiledialog1.FileName != "")
    {
        //Saves the image via filestram created by Openfile method.
        System.IO.FileStream fs =
                (System.IO.FileStream)savefiledialog1.OpenFile();
        //Saves the image in the appropriate Imageformat based upon the
        //file type selected in the dialog box.
        //NOTE that the filterIndex property is one-based.
        switch(savefiledialog1.FilterIndex)
        {
            case 1 :
                this.saveAsToolStripMenuItem.Image.Save(fs,
                    System.Drawing.Imaging.ImageFormat.Jpeg);
                break;

            case 2:
                this.saveAsToolStripMenuItem.Image.Save(fs,
                    System.Drawing.Imaging.ImageFormat.Bmp);
                break;

            case 3:
                this.saveAsToolStripMenuItem.Image.Save(fs,
                    System.Drawing.Imaging.ImageFormat.Gif);
                break;

            case 4:
                this.saveAsToolStripMenuItem.Image.Save(fs,
                    System.Drawing.Imaging.ImageFormat.Png);
                break;
        }

        fs.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中的某些位置应该具有类似

的内容
partial class MainWindow : Window
{

     List<someImageClass> images;// <------this here 
     /*someImageClass should store the paths to the images */

     string currentimage; //maintain this or if you are binding to listbox or other list control
                          //you will be able to use ((someImageClass) listcontrol.selectedItem).path



     private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
     {

          //so in here you can
          if(!File.Exists(currentimage.path))
          {
              /*show dialog*/ 
              **EDIT** missed save via dialog part

              if(savelog.ShowDialog ?? false == true)
                {
                     //depends on how you are storing image data
                     // see below
                     imageclassinstance.save(savelog.FileName);
                }

          }
          else
          {
            using(FileStream fs = new FileStream(currentimage.path,FileMode.CreateNew,FileAccess.ReadWrite))
            using(BinaryWriter bw = new BinaryWriter(bw))
                     {
                               bw.WriteByte();//loop this images are not 1 byte

                              /*you don't really need binarywriter it will depend on what you 

                               are using to store the image data. some types have 

                               built in save functions*/
                     }
           }
         .....
     }


}