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

C# 通过对话框将PictureBox图像写入光盘,c#,picturebox,savefiledialog,C#,Picturebox,Savefiledialog,我想知道是否有人能帮我排除故障 我是编程新手,一直在寻找自己的照片查看器。我觉得它大部分都很好用 我的问题是,我希望能够将打开到查看器中的图像保存为新图像。我有一些按钮来更改图片。我的问题是我有一个保存对话框,当定义完保存后,按save,它只会重新打开一个新的保存对话框,不会写入光盘。我的问题是,如何才能真正写入文件,并防止每次尝试使用对话框保存时都显示“另存为” 这是我的密码: private void saveAsToolStripMenuItem_Click(object sender,

我想知道是否有人能帮我排除故障

我是编程新手,一直在寻找自己的照片查看器。我觉得它大部分都很好用

我的问题是,我希望能够将打开到查看器中的图像保存为新图像。我有一些按钮来更改图片。我的问题是我有一个保存对话框,当定义完保存后,按save,它只会重新打开一个新的保存对话框,不会写入光盘。我的问题是,如何才能真正写入文件,并防止每次尝试使用对话框保存时都显示“另存为”

这是我的密码:

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();
    }
}

在代码中的某些地方,您应该

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*/
                     }
           }
         .....
     }


}
部分类主窗口:窗口
{

列出图像;//您需要存储文件名。因此,下次保存时,您已经知道将其放置在何处。请使用变量。我是编程新手,因此,如果您有一个示例说明您的意思,它会告诉我。@bartoszKP,我使用的是Visual Studio Express。事件处理程序存储在“InitializeComponent()下”此初始化组件()具有以下代码://saveFileDialog1//This.saveFileDialog1.FileName=“untitled”;This.saveFileDialog1.Filter=“JPEG文件(.jpg)|.jpg | PNG文件(.PNG)|.PNG | BMP文件(.BMP)|.BMP |所有文件“+”s()|*”;this.saveFileDialog1.InitialDirectory=“Libraries\\Pictures”;this.saveFileDialog1.Title=“另存为”this.saveFileDialog1.FileOk+=new System.ComponentModel.CancelEventHandler(this.saveFileDialog1\u FileOk);您可以编辑您的帖子。请不要像以前那样通过破坏其他人的编辑来编辑。通过puretppc编辑是正确的-您不应该在标题中使用标记。感谢您的提示。我完全知道我可以做什么。如果您不打算帮我解决我的问题,请不要再发表评论。我在这里学习编程,不要受到冒犯关于我为发表我的文章所做的事情。