{"record":{"id":"682b223737dcb283","repo":"dotnet/AspNetCore.Docs","slug":"qr-code-size-must-be-less-than-maxqrsize","errorCode":null,"errorMessage":"QR code size must be less than {MaxQrSize}.","messagePattern":"QR code size must be less than (.+?)\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md","lineNumber":285,"sourceCode":"\n`Workers/QRGenerator.razor.cs`:\n\n```csharp\nusing System.Runtime.InteropServices.JavaScript;\nusing System.Runtime.Versioning;\nusing QRCoder;\n\n[SupportedOSPlatform(\"browser\")]\npublic partial class QRGenerator\n{\n    private static readonly int MaxQrSize = 20;\n\n    [JSExport]\n    internal static byte[] Generate(string text, int qrSize)\n    {\n        if (qrSize >= MaxQrSize)\n        {\n            throw new Exception($\"QR code size must be less than {MaxQrSize}.\");\n        }\n\n        var generator = new QRCodeGenerator();\n        QRCodeData data = generator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q);\n        var qrCode = new BitmapByteQRCode(data);\n    \n        return qrCode.GetGraphic(qrSize);\n    }\n}\n```\n\nCreate a matching Razor component file (`.razor`) to act as an empty stub so that the build packs the worker script alongside the component assets:\n\n`Workers/QRGenerator.razor`:\n\n```razor\n// dummy file to let blazor handle Worker.razor.js file loading\n```","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/blazor/blazor-with-dotnet-on-web-workers.md#L267-L303","documentation":"The Blazor web-worker QR sample guards `Generate(text, qrSize)` with `if (qrSize >= MaxQrSize) throw` where MaxQrSize is 20. qrSize is the pixels-per-module passed to QRCoder's `GetGraphic`; large values blow up worker memory and render time, so the sample caps it.","triggerScenarios":"Calling the exported `QRGenerator.Generate(text, size)` with `size >= 20`. The check is `>=`, so the maximum allowed value is 19.","commonSituations":"Caller passes a default or guessed size (e.g. 25 or 32); UI control lets the user pick any number; size derived from device pixel ratio without clamping.","solutions":["Pass a size strictly less than 20 (e.g. 10–15 for typical QR codes).","Clamp the caller-supplied value before invoking the worker: `size = Math.min(size, 19)`.","Validate input in the message handler and surface a friendly error to the UI rather than letting the throw propagate."],"exampleFix":"// before\nconst bytes = assemblyExports.QRGenerator.Generate(text, 32);\n\n// after\nconst size = Math.min(requestedSize, 19);\nconst bytes = assemblyExports.QRGenerator.Generate(text, size);","handlingStrategy":"validation","validationCode":"function safeQrSize(requested: number, maxExclusive = 20): number {\n  if (!Number.isFinite(requested) || requested <= 0) throw new RangeError('qrSize must be a positive number');\n  return Math.min(Math.floor(requested), maxExclusive - 1);\n}","typeGuard":"function isValidQrSize(size: number, maxExclusive = 20): boolean {\n  return Number.isInteger(size) && size > 0 && size < maxExclusive;\n}","tryCatchPattern":"// Worker side already throws; guard on the caller:\ntry {\n  await postToWorker({ command: 'generateQR', text, size: safeQrSize(size) });\n} catch (err) {\n  if (/QR code size/.test(err.message)) showUserError('Pick a smaller QR size.');\n}","preventionTips":["Clamp user-supplied size before sending to the worker.","Bound the UI input (e.g. `<input type=\"number\" min=\"1\" max=\"19\">`).","Share the MaxQrSize constant between C# and TS."],"tags":["blazor","web-worker","qr","validation","bounds"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}