保存图片时出错GDI+一般性

king19840811 2008-12-15 12:25:57
FileInfo fileInfo = new FileInfo(this.pictureopenFileDialog.FileName);
string szExt = fileInfo.Extension;

//创建一个内存位图
Bitmap bmp = new Bitmap(this.pictureBox.Image.Width, this.pictureBox.Image.Height);

//把pictureBox里面的图象绘制到内存位图上
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(this.pictureBox.Image, 0, 0);
g.Dispose();

//保存内存位图为文件
string szFileName = Application.StartupPath + "\\..\\..\\userpicture\\我的图片" + szExt;
FileInfo fileInfo1 = new FileInfo(szFileName);
if (!fileInfo1.Directory.Exists)
fileInfo1.Directory.Create();
bmp.Save(fileInfo1.Directory.FullName); GDI+ 中发生一般性错误
bmp.Dispose();

详细错误:。+ Length “fileInfo1.Length”引发了“System.IO.FileNotFoundException”类型的异常 long {System.IO.FileNotFoundException}


通过单步已经确定文件夹路径正确。但到了调用save方法时就会出异常。保存不到指定文件夹内。提示如上错误
...全文
151 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2008-12-15
  • 打赏
  • 举报
回复
fileInfo1.Directory.FullName

着是路径 不是文件名

Bitmap.Save(string filename)
是文件名
cpio 2008-12-15
  • 打赏
  • 举报
回复
有这么麻烦吗?


string szFileName = Application.StartupPath + "\\..\\..\\userpicture\\我的图片" + szExt;
FileInfo fileInfo1 = new FileInfo(szFileName);
if (!fileInfo1.Directory.Exists)
fileInfo1.Directory.Create();

this.pictureBox.Image.Save(szFileName);
simonezhlx 2008-12-15
  • 打赏
  • 举报
回复
调用Image自身的Save 就够了,楼主结帖吧
周公 2008-12-15
  • 打赏
  • 举报
回复

private void Button5_Click(System.Object sender, System.EventArgs e)
{
try
{
if (image1 != null)
{
image1.Save("c:\\myBitmap.bmp");
Button5.Text = "Saved file.";
}
}
catch(Exception)
{
MessageBox.Show("There was a problem saving the file." +
"Check the file permissions.");
}

}
周公 2008-12-15
  • 打赏
  • 举报
回复
确认你要保存的那个文件路径是什么?对应的文件夹是否存在。
孟子E章 2008-12-15
  • 打赏
  • 举报
回复
FileNotFoundException

已经说的很明白了

路径不存在或者没有权限
king19840811 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cpio 的回复:]
//创建一个内存位图
Bitmap bmp = new Bitmap(this.pictureBox.Image.Width, this.pictureBox.Image.Height);

//把pictureBox里面的图象绘制到内存位图上
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(this.pictureBox.Image, 0, 0);
g.Dispose();

这一段用不上
[/Quote]
恩删了果然能行。但是为什么会出现这种错误呢
cpio 2008-12-15
  • 打赏
  • 举报
回复
//创建一个内存位图
Bitmap bmp = new Bitmap(this.pictureBox.Image.Width, this.pictureBox.Image.Height);

//把pictureBox里面的图象绘制到内存位图上
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(this.pictureBox.Image, 0, 0);
g.Dispose();


这一段用不上

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧