LorisYounger/VPet · error · InvalidDataException

Invalid PNG/APNG signature.

Error message

Invalid PNG/APNG signature.

What it means

Thrown by ParseApng while opening an APNG file for frame parsing: the file's first bytes are read and compared against the canonical PNG signature (137 80 78 71 13 10 26 10). This is a generic validation guard, and the failing input is the sprite/animation file at `path` — typically a file that is not a PNG at all (a JPEG/WebP or other format renamed to .png, a truncated download, or a zero-byte/corrupted file). It signals that APNG frame extraction cannot proceed and the animation cannot be decoded as a PNG/APNG.

Solutions

  1. Point Path at a genuine PNG/APNG file.
  2. Verify the first 8 bytes are the PNG signature before loading.
  3. Re-export the image as PNG.
  4. Re-download the asset if the file is an HTML/error-page stub.

Example fix

// before
new APNGAnimation(imagePath); // actually a JPEG
// after
var bytes = new byte[8];
using (var fs = File.OpenRead(imagePath)) fs.Read(bytes, 0, 8);
bool isPng = bytes.SequenceEqual(new byte[]{137,80,78,71,13,10,26,10});
if (isPng) new APNGAnimation(imagePath);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsPng(string p) { var b = new byte[8]; using var fs = File.OpenRead(p); return fs.Read(b,0,8)==8 && b.AsSpan().SequenceEqual(stackalloc byte[]{137,80,78,71,13,10,26,10}); }

Type guard

bool IsPngFile(string path) => File.Exists(path) && IsPng(path);

Try / catch

try { var anim = new APNGAnimation(path); } catch (InvalidDataException ex) when (ex.Message.Contains("signature")) { Log.Warn($"{path} is not a PNG"); }

Prevention

When it happens

Trigger: Path points to a JPEG/GIF/WebP/BMP, an HTML error page saved as .png, or a zero-byte file; passed to the APNGAnimation public constructor.

Common situations: Extension renamed manually; CDN/download returned an error page; placeholder file; asset pipeline saved wrong format with .png extension.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of LorisYounger/VPet@ffb9cc2a85 (2026-09-15). Data as JSON: /api/errors/75145cb83c60156a. Report an issue: GitHub.

Appendix: source

Thrown at VPet-Simulator.Core/Graph/APNGAnimation.cs:249

                            canvas.DrawBitmap(previousCanvas, 0, 0);
                        }
                        break;
                }
            }

            using var image = SKImage.FromBitmap(combinedBitmap);
            using var data = image.Encode(SKEncodedImageFormat.Png, 100);
            using var stream = File.Open(SpriteSheetPath, FileMode.Create, FileAccess.Write, FileShare.Read);
            data.SaveTo(stream);
        }

        private static ParsedApng ParseApng(string path)
        {
            using var stream = File.OpenRead(path);
            using var reader = new BinaryReader(stream);
            var signature = reader.ReadBytes(8);
            if (signature.Length != 8 || !MatchesSignature(signature))
                throw new InvalidDataException("Invalid PNG/APNG signature.");

            var result = new ParsedApng();
            ApngFrameData? currentFrame = null;
            bool imageDataStarted = false;

            while (stream.Position < stream.Length)
            {
                uint length = ReadUInt32BigEndian(reader);
                string type = Encoding.ASCII.GetString(reader.ReadBytes(4));
                byte[] data = reader.ReadBytes((int)length);
                ReadUInt32BigEndian(reader);

                switch (type)
                {
                    case "IHDR":
                        result.IhdrTemplate = data;
                        result.CanvasWidth = (int)ReadUInt32BigEndian(data, 0);
                        result.CanvasHeight = (int)ReadUInt32BigEndian(data, 4);

View on GitHub (pinned to ffb9cc2a85)