{"record":{"id":"b8bfad5e253af521","repo":"NickeManarin/ScreenToGif","slug":"unable-to-determine-image-format","errorCode":null,"errorMessage":"Unable to determine image format","messagePattern":"Unable to determine image format","errorType":"exception","errorClass":"NoNullAllowedException","httpStatus":null,"severity":"error","filePath":"ScreenToGif/ImageUtil/ImageMethods.cs","lineNumber":1372,"sourceCode":"        // Check the image format to determine what format\n        // the image will be saved to the memory stream in\n        var guidToImageFormatMap = new Dictionary<Guid, ImageFormat>()\n        {\n            {ImageFormat.Bmp.Guid,  ImageFormat.Bmp},\n            {ImageFormat.Gif.Guid,  ImageFormat.Png},\n            {ImageFormat.Icon.Guid, ImageFormat.Png},\n            {ImageFormat.Jpeg.Guid, ImageFormat.Jpeg},\n            {ImageFormat.Png.Guid,  ImageFormat.Png}\n        };\n\n        using (var gifImg = Image.FromFile(fileName, true))\n        {\n            var imageGuid = gifImg.RawFormat.Guid;\n\n            var imageFormat = (from pair in guidToImageFormatMap where imageGuid == pair.Key select pair.Value).FirstOrDefault();\n\n            if (imageFormat == null)\n                throw new NoNullAllowedException(\"Unable to determine image format\");\n\n            //Get the frame count\n            var dimension = new FrameDimension(gifImg.FrameDimensionsList[0]);\n            var frameCount = gifImg.GetFrameCount(dimension);\n\n            //Step through each frame\n            for (var i = 0; i < frameCount; i++)\n            {\n                //Set the active frame of the image and then\n                gifImg.SelectActiveFrame(dimension, i);\n\n                //write the bytes to the tmpFrames array\n                using (var ms = new MemoryStream())\n                {\n                    gifImg.Save(ms, imageFormat);\n                    tmpFrames.Add(ms.ToArray());\n                }\n            }","sourceCodeStart":1354,"sourceCodeEnd":1390,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif/ImageUtil/ImageMethods.cs#L1354-L1390","documentation":"ImageMethods loads an image with Image.FromFile(fileName, true) and maps its RawFormat.Guid against a fixed whitelist (Bmp, Gif, Icon, Jpeg, Png). If the GUID matches none, it throws NoNullAllowedException(\"Unable to determine image format\"). The whitelist deliberately excludes TIFF, WMF, EMF, EXIF, WebP and memory-bitmap formats.","triggerScenarios":"Opening/importing an image whose RawFormat.Guid is not in {Bmp, Gif, Icon, Jpeg, Png}. Common for .tiff/.tif, .webp, .wmf, .emf, .exif, or images whose RawFormat resolves to ImageFormat.MemoryBmp.","commonSituations":"User drops a TIFF/WEBP/WMF file into the editor; image saved by another tool with a non-standard raw format GUID; corrupted file whose header GUID is garbage; screenshot tools that emit EXIF-tagged JPEGs that report a different GUID.","solutions":["Convert the source image to PNG or JPEG before importing.","Extend guidToImageFormatMap with TIFF/WMF/EXIF entries if those formats must be supported.","Pre-validate the format and show a user-friendly message listing supported formats instead of throwing NoNullAllowedException.","If the file is corrupt, re-export it from the original source."],"exampleFix":"// before\nvar guidToImageFormatMap = new Dictionary<Guid, ImageFormat>()\n{\n    {ImageFormat.Bmp.Guid,  ImageFormat.Bmp},\n    {ImageFormat.Gif.Guid,  ImageFormat.Png},\n    {ImageFormat.Icon.Guid, ImageFormat.Png},\n    {ImageFormat.Jpeg.Guid, ImageFormat.Jpeg},\n    {ImageFormat.Png.Guid,  ImageFormat.Png}\n};\n...\nif (imageFormat == null)\n    throw new NoNullAllowedException(\"Unable to determine image format\");\n\n// after\nvar guidToImageFormatMap = new Dictionary<Guid, ImageFormat>()\n{\n    {ImageFormat.Bmp.Guid,  ImageFormat.Bmp},\n    {ImageFormat.Gif.Guid,  ImageFormat.Png},\n    {ImageFormat.Icon.Guid, ImageFormat.Png},\n    {ImageFormat.Jpeg.Guid, ImageFormat.Jpeg},\n    {ImageFormat.Png.Guid,  ImageFormat.Png},\n    {ImageFormat.Tiff.Guid, ImageFormat.Png},\n    {ImageFormat.Exif.Guid, ImageFormat.Jpeg}\n};\n...\nif (imageFormat == null)\n    throw new NotSupportedException($\"Unsupported image format (Guid {imageGuid}). Supported: BMP, GIF, ICO, JPEG, PNG, TIFF.\");","handlingStrategy":"validation","validationCode":"using var probe = Image.FromFile(fileName, true);\nvar supported = new[] { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Icon, ImageFormat.Jpeg, ImageFormat.Png }\n    .Any(f => f.Guid == probe.RawFormat.Guid);\nif (!supported) /* reject with a clear message listing supported formats */","typeGuard":"static bool IsSupportedRawFormat(Image img) =>\n    ImageFormat.Bmp.Guid == img.RawFormat.Guid ||\n    ImageFormat.Gif.Guid == img.RawFormat.Guid ||\n    ImageFormat.Icon.Guid == img.RawFormat.Guid ||\n    ImageFormat.Jpeg.Guid == img.RawFormat.Guid ||\n    ImageFormat.Png.Guid == img.RawFormat.Guid;","tryCatchPattern":"try { return ImageMethods.ImportFromFile(fileName); }\ncatch (NoNullAllowedException ex) when (ex.Message.Contains(\"image format\"))\n{ /* prompt user to convert to PNG/JPEG */ }","preventionTips":["Filter the open-file dialog to *.png;*.jpg;*.jpeg;*.gif;*.bmp;*.ico.","Pre-validate RawFormat.Guid before entering the frame loop.","If TIFF/EXIF must be supported, extend the whitelist map deliberately."],"tags":["image-processing","system-drawing","import","format-detection"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}