使用 SaveFileDialog 后,在 Bitmap.Save() 的 GDI+ 处发生一般错误
- 作者: 老司饥
- 来源: 51数据库
- 2022-10-20
问题描述
我使用以下代码块,并在 using 块中添加了更多代码:
I use the following code block with some more code inside the using block:
using (System.Drawing.Bitmap tempImg =
(System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap))
{
// ...
tempImg.Save("..\..\testdata\tempImg.bmp", ImageFormat.Bmp);
// ...
}
但我仍然收到错误:
GDI+ 出现一般错误
A Generic Error occured at GDI+
只有在我做了一些与 using 块内的代码无关的操作之后.在其他时候,这很有效.tempImg.bmp 也是一个临时文件,所以我删除了 using 块本身中的 tempImg.bmp.
only after I make some action which is not related to the code inside the using block. In other times this works well. Also the tempImg.bmp is a temporary file, so I delete the tempImg.bmp within the using block itself.
由于 tempImg 在 using 里面并且它被处理了,我认为应该解决锁定问题.
Since the tempImg is inside the using and this it's disposed, I think the locking problem should be solved.
有人可以让我知道这段代码中的错误是什么吗?
Can someone please let me know what is the mistake in this code?
System.Drawing.Image 到 System.Drawing.Bitmap 作为 tempImg 的类型.
System.Drawing.Image to System.Drawing.Bitmap as the type of tempImg.
我已经确定只有在创建 SaveFileDialog 并且用户单击保存"后才会收到此错误.
I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'.
推荐答案
我终于可以找到我的代码有什么问题了,我想在这里提一下,因为我认为它可能对某人有用....
Finally I could find what was wrong in my code and would like to mention it here as I think it may be useful to someone....
因为我在 tempImg.Save 中给出了一个相对路径,在用户点击 SaveFileDialog 中的Save"后,tempImg.Save 的实际路径变为:
As I have given a relative path in tempImg.Save, and after the user clicks 'Save' in SaveFileDialog, the actual path for tempImg.Save become :
SaveFileDialog指定的路径+相对路径
Path specified by SaveFileDialog + the relative path
自动.
因此,如果路径不存在,则会发生此错误.
Thus if the path does not exist, this error occurs.
谢谢大家的回答.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
