{"record":{"id":"9ce2e07c76c70a77","repo":"nopSolutions/nopCommerce","slug":"cannot-decode-picture-binary-file-name-filename","errorCode":null,"errorMessage":"Cannot decode picture binary (file name: {fileName})","messagePattern":"Cannot decode picture binary \\(file name: (.+?)\\)","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Media/PictureService.cs","lineNumber":1030,"sourceCode":"                using var input = new MemoryStream(pictureBinary);\r\n                using var codec = SKCodec.Create(input);\r\n                image = AutoOrient(SKBitmap.Decode(codec), codec.EncodedOrigin);\r\n            }\r\n            else\r\n                image = SKBitmap.Decode(pictureBinary);\r\n\r\n            //resize the image in accordance with the maximum size\r\n            if (Math.Max(image.Height, image.Width) <= _mediaSettings.MaximumImageSize)\r\n                return Task.FromResult(pictureBinary);\r\n\r\n            var format = GetImageFormatByMimeType(mimeType);\r\n            pictureBinary = ImageResize(image, format, _mediaSettings.MaximumImageSize);\r\n\r\n            return Task.FromResult(pictureBinary);\r\n        }\r\n        catch (Exception exc)\r\n        {\r\n            throw new NopException($\"Cannot decode picture binary (file name: {fileName})\", exc);\r\n        }\r\n    }\r\n\r\n    /// <summary>\r\n    /// Get product picture (for shopping cart and order details pages)\r\n    /// </summary>\r\n    /// <param name=\"product\">Product</param>\r\n    /// <param name=\"attributesXml\">Attributes (in XML format)</param>\r\n    /// <returns>\r\n    /// A task that represents the asynchronous operation\r\n    /// The task result contains the picture\r\n    /// </returns>\r\n    public virtual async Task<Picture> GetProductPictureAsync(Product product, string attributesXml)\r\n    {\r\n        ArgumentNullException.ThrowIfNull(product);\r\n\r\n        //first, try to get product attribute combination picture\r\n        var combination = await _productAttributeParser.FindProductAttributeCombinationAsync(product, attributesXml);\r","sourceCodeStart":1012,"sourceCodeEnd":1048,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Media/PictureService.cs#L1012-L1048","documentation":"Thrown by PictureService.ValidatePictureAsync, which wraps the entire image decode+resize in try/catch. It decodes the binary via SkiaSharp (SKCodec/SKBitmap.Decode) and resizes if it exceeds MaximumImageSize. Any exception from decode/resizing is rethrown as NopException with the file name. The root cause is inside the inner exception — typically an unsupported/corrupt image format.","triggerScenarios":"Uploading a file whose bytes are not a decodable image (wrong MIME type, corrupt file, unsupported codec such as certain TIFF/HEIF, or a zero-length file) so SKBitmap.Decode/SKCodec.Create throws.","commonSituations":"User uploads a renamed non-image (e.g. .jpg that is actually a PDF); truncated/corrupt upload; SkiaSharp build lacking the codec for the format; very large image causing decode failure; empty stream.","solutions":["Inspect the inner exception (exc) for the exact SkiaSharp error and the true cause.","Validate the file is a real image of a supported type (JPEG/PNG/GIF/BMP/WebP) before uploading.","Re-save/convert the source image to a supported format and retry.","Ensure the deployed SkiaSharp native binaries match the runtime platform so codecs load.","Reject zero-length or undersized uploads at the client/API boundary."],"exampleFix":"// before\npublic virtual Task<byte[]> ValidatePictureAsync(byte[] pictureBinary, string mimeType, string fileName)\n{\n    try { /* decode + resize */ }\n    catch (Exception exc)\n    {\n        throw new NopException($\"Cannot decode picture binary (file name: {fileName})\", exc);\n    }\n}\n\n// after - pre-validate before decoding\nif (pictureBinary is null || pictureBinary.Length == 0)\n    throw new NopException($\"Empty picture binary (file name: {fileName})\");\nusing var probe = new MemoryStream(pictureBinary);\nusing var codec = SKCodec.Create(probe);\nif (codec is null)\n    throw new NopException($\"Unsupported image format (file name: {fileName})\");","handlingStrategy":"validation","validationCode":"// Validate the binary is a decodable image of a supported type before ValidatePictureAsync\nif (pictureBinary is null || pictureBinary.Length == 0)\n    return Error($\"Empty picture binary (file name: {fileName})\");\nusing var probe = new MemoryStream(pictureBinary);\nusing var codec = SKCodec.Create(probe);\nif (codec is null || !_supportedMimes.Contains(codec.EncodedFormat.ToString().ToLower()))\n    return Error($\"Unsupported or corrupt image (file name: {fileName})\");","typeGuard":"static bool IsDecodableImage(byte[] bytes)\n{\n    if (bytes is null || bytes.Length == 0) return false;\n    try { using var ms = new MemoryStream(bytes); return SKCodec.Create(ms) is not null; }\n    catch { return false; }\n}","tryCatchPattern":"try { var validated = await _pictureService.ValidatePictureAsync(binary, mime, fileName); }\ncatch (NopException ex) when (ex.Message.StartsWith(\"Cannot decode picture binary\"))\n{ /* inspect ex.InnerException for the SkiaSharp cause; reject the upload with a user-facing message */ }","preventionTips":["Reject zero-length and wrong-extension uploads at the API boundary.","Limit uploads to supported formats (JPEG/PNG/GIF/BMP/WebP).","Keep the SkiaSharp native binaries matched to the runtime platform.","Log the inner exception to diagnose corrupt or unsupported files."],"tags":["media","image","skiasharp","upload","validation","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}