简体   繁体   English

Bitmap.Save() 抛出通用 GDI+ 错误或 ArgumentException

[英]Bitmap.Save() throws generic GDI+ error or ArgumentException

This is my code:这是我的代码:

save.Save(Environment.CurrentDirectory+ "/Content/map.png");
save.Dispose();

I have no idea why this throws a generic GDI+ error.我不知道为什么这会引发通用 GDI+ 错误。 If I change it to如果我把它改成

save.Dispose();
save.Save(Environment.CurrentDirectory+ "/Content/map.png");

it will throw a ArgumentException because of a invalid parameter.由于参数无效,它将抛出 ArgumentException。

try saving the path to a FileInfo object尝试将路径保存到 FileInfo 对象

FileInfo lInfo = new FileInfo(Environment.CurrentDirectory + "/Content/map.png");
save.Save(lInfo.FullName);
save.Dispose();

this will ensure that the filename is in valid format.这将确保文件名的格式有效。 Also make sure you have administrative rights to that directory, or that visual studio is running as administrator.还要确保您对该目录具有管理权限,或者 Visual Studio 以管理员身份运行。

This does NOT work on monogame, if you are trying to save a texture2D to a bitmap, just FYI.这不适用于单游戏,如果您尝试将 texture2D 保存到位图,仅供参考。

Are you sure that你确定吗

`FileInfo lInfo = new FileInfo(Environment.CurrentDirectory + "/Content/map.png");`

will work?将工作? as far as i know, you have to use据我所知,你必须使用

`FileInfo lInfo = new FileInfo(Path.Combine(Environment.CurrentDirectory, @"content\map.png"));`

or或者

`FileInfo lInfo = new FileInfo(Path.Combine(Environment.CurrentDirectory, "//content//map.png"))`

Use Path.Combine with direrctory and file and this method will return the path.使用Path.Combine与目录和文件,此方法将返回路径。

Example:例子:

string file = "map.png";
string dir = Environment.CurrentDirectory;
string path = System.IO.Path.Combine(dir, file);

save.Save(path);
save.Dispose();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM