codebude / QRCoder

A pure C# Open Source QR Code implementation
MIT License
4.61k stars 1.1k forks source link

XamlQRCode crash #145

Closed fvaillancourt closed 5 years ago

fvaillancourt commented 6 years ago

Type of issue

[X] Bug
[ ] Question (e.g. about handling/usage)
[ ] Request for new feature/improvement

Expected Behavior

XamlQRCode should not crash ...

Current Behavior

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.'

Possible Solution (optional)

XamlQRCode loops on the size adding doubles together hoping to get to the width/height. Adding doubles don't always round to the value you would normally expect. You should loop on the QRCode size and add pixel for placement instead.

double x = 0;
double y;
for (int xi = offsetModules; xi < QrCodeData.ModuleMatrix.Count - offsetModules; ++xi)
{
    y = 0;
    for (int yi = offsetModules; yi < QrCodeData.ModuleMatrix.Count - offsetModules; ++yi)
    {
        if (this.QrCodeData.ModuleMatrix[yi + offsetModules][xi + offsetModules])
        {
            group.Children.Add(new RectangleGeometry(new Rect(x, y, unitsPerModule, unitsPerModule)));
        }
        y += unitsPerModule;
    }

    x += unitsPerModule;
}

Steps to Reproduce (for bugs)

var generator = new QRCodeGenerator();
var data = generator.CreateQrCode("Something that ends up with a size 41 QRCode", QRCodeGenerator.ECCLevel.Q);
var xaml = new XamlQRCode(data);
Code = xaml.GetGraphic(new Size(200, 200), Brushes.Black, Brushes.White);

Your Environment

Version Used: QRCoder 1.3.3 from NuGet

codebude commented 5 years ago

Fixed in https://github.com/codebude/QRCoder/pull/147