我的项目在 Electronjs 中。我必须根据扩展名获取文件图标。我的 c# 脚本运行良好,直到将其打包成 exe。打包后会报错。
这里的错误:
Error: Command failed: C:\my-
project\resources\app\iconExtractor\bin\Debug\netcoreapp3.1\iconExtractor.exe
Unhandled exception. System.Runtime.InteropServices.ExternalException (0x80004005): A generic error
occurred in GDI+.
at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ExtractPngOfExtIcon.Program.Main(String[] args) in C:\Users\syrym\source\repos\iconExtractor\Program.cs:line 29
at ExtractPngOfExtIcon.Program.<Main>(String[] args)
at ChildProcess.exithandler (child_process.js:308:12)
at ChildProcess.emit (events.js:203:13)
at maybeClose (internal/child_process.js:1021:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
killed: false,
code: 3762504530,
signal: null,
cmd: 'C:\\my-project\\resources\\app\\iconExtractor\\bin\\Debug\\netcoreapp3.1\\iconExtractor.exe'
}
这里是代码:
public static async System.Threading.Tasks.Task Main(string[] args)
{
string files = await fileList.LoadJson();
string[] arrFiles = (files.TrimStart('[').TrimEnd(']')).Split(',');
foreach (var file in arrFiles)
{
var ext = file.Substring(file.IndexOf('.')+1);
IntPtr hIcon = GetJumboIcon(GetIconIndex("*." + ext.TrimEnd('"')));
// from native to managed
using (Icon ico = (Icon)Icon.FromHandle(hIcon).Clone())
{
// save to file (or show in a picture box)
Console.WriteLine(ico);
ico.ToBitmap().Save("./assets/" + ext.TrimEnd('"') + ".png", ImageFormat.Png);
}
Shell32.DestroyIcon(hIcon);
}
}