【问题标题】:C#: error when saving an image to the Documents folderC#:将图像保存到 Documents 文件夹时出错
【发布时间】:2015-11-05 12:12:47
【问题描述】:

我的 C# 表单上显示了一个图像。当用户单击“保存图像”按钮时,它会弹出一个 Visual Basic 输入框。我正在尝试向我的表单添加功能,允许用户在通过 Visual Basic 输入框输入图像名称时保存图像。

首先,我添加了这段代码,

private void save_image(object sender, EventArgs e)
    {
        String picname;
        picname = Interaction.InputBox("Please enter a name for your Image");            
        pictureBit.Save("C:\\"+picname+".Png");                      
        MessageBox.Show(picname +" saved in Documents folder");
    } 

但是,当我运行程序并单击保存按钮时,它给出了以下异常:“System.Drawing.dll 中发生了类型为 'System.Runtime.InteropServices.ExternalException' 的未处理异常”

然后我对代码添加了一些更改,使其看起来像这样,

private void save_image(object sender, EventArgs e)
    {

        SaveFileDialog savefile = new SaveFileDialog();            
        String picname;           
        picname = Interaction.InputBox("Please enter a name for your Image");
        savefile.FileName = picname + ".png";
        String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        using (Stream s = File.Open(savefile.FileName, FileMode.Create))
        {
            pictureBit.Save(s, ImageFormat.Png);
        }
        //pictureBit.Save("C:\\pic.Png");           
        MessageBox.Show(picname);                                
    }

当我运行此代码时,它不再给出异常,而是将图像保存在我的 c#->bin->debug 文件夹中。我知道这可能不是理想的方法,但我如何设置它的路径,以便将图像保存在文档文件夹中。

【问题讨论】:

  • 你没有设置路径
  • 如何设置路径?这就是我不明白的
  • 试试savefile.FileName = string.Format("{0}\\{1}.png", path, picname);

标签: c# vb.net bitmap filepath inputbox


【解决方案1】:
String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
savefile.FileName = path + "\\" + picname + ".png";

显示对话框的其他工作示例:

SaveFileDialog savefile = new SaveFileDialog();
String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

savefile.InitialDirectory = path;
savefile.FileName = "picname";
savefile.Filter = "PNG images|*.png";
savefile.Title = "Save as...";
savefile.OverwritePrompt = true;

if (savefile.ShowDialog() == DialogResult.OK)
{
    Stream s = File.Open(savefile.FileName, FileMode.Create);
    pictureBit.Save(s,ImageFormat.Png);
    s.Close();
}

其他存档示例:

if (savefile.ShowDialog() == DialogResult.OK)
{
    pictureBit.Save(savefile.FileName,ImageFormat.Png);
}

【讨论】:

  • 非常感谢,在添加您的代码并稍作更改后,它就可以工作了。它应该是两个反斜杠而不是一个。
  • 当我决定保存图像时,首先,我尝试使用与您的第二个示例非常相似的方法。但是,它给出了一个异常,说“System.Windows.Forms.dll 中发生了'System.Threading.ThreadStateException' 类型的未处理异常”,我不明白如何修复,所以我尝试了 VB 输入框方法。你知道如何解决这个异常吗?
  • SaveFileDialog savefile = new SaveFileDialog(); savefile.FileName = "img.png"; savefile.Filter = "PNG 文件 (.png)|.png|所有文件 (.)|*.*"; if (savefile.ShowDialog() == DialogResult.OK) { using (Stream s = File.Open(savefile.FileName, FileMode.Create)) { pictureBit.Save(s, ImageFormat.Png); } }
  • 很抱歉它看起来很乱我不知道如何在评论部分编辑它
  • 给我看一下pictureBit的定义
【解决方案2】:

您没有设置路径,请参阅MSDN 了解更多信息。

【讨论】:

    猜你喜欢
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    相关资源
    最近更新 更多