【发布时间】: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