Cannot save array of png images: "generic error in GDI+"


My project is in Electronjs. And i have to get icons of files according to extension. My c# script working well till packaging it into exe. It throws an error after packaging.

Here the error:

Error: Command failed: C:\my- 
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'


and here the code:

    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)
                ico.ToBitmap().Save("./assets/" + ext.TrimEnd('"') + ".png", ImageFormat.Png);
asked on Stack Overflow Jan 23, 2020 by Syrym • edited Jan 23, 2020 by Ian Kemp

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0