user4612290
user4612290

Reputation: 321

How to re-size QRCode image without losing resolution?

I'm generating a QR code using the QR code library "MessagingToolkit.QRCode.Codec" but it's generating a large size QR code and I'm trying to resize it to 70x70px, however the generated image's resolution is not good.

protected void BtnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += PrintPage;
            pd.Print();
        }

        private void PrintPage(object o, PrintPageEventArgs e)
        {
            QRCodeEncoder encoder = new QRCodeEncoder();
            String EmployeeId= this.Request.QueryString.Get("EmployeeId").ToString();
            Bitmap img = new Bitmap (encoder.Encode(EmployeeId), new Size(70,70));


            Point loc = new Point(100, 100);
            e.Graphics.DrawImage(img, loc);
        }

How can I resize this QRCode without losing the resolution ?

EDIT:

Is it possible to create the QRCode from the beginning with the required size ?

Upvotes: 0

Views: 13137

Answers (2)

Mark White
Mark White

Reputation: 1

If you compare these QR Codes we have created in a graphics program you will see the difference in resolution. The first (2488) has a scale of 35 and the second (2490) has a scale of 40. What this means is that each module is either 35 or 40 pixels square. If you were making a QR Code for a billboard (as opposed to say a business card) you would need to ensure that the resolution of each module was high enough that it doesn't 'pixelate' (blur) when printed really large. The down side is that you are creating a larger image which will take up more memory if (like Codacast) you are creating (and saving) lots of them - hope this helps :-)

http://www.codacast.com/qrimages/2488.png http://www.codacast.com/qrimages/2490.png

Upvotes: 0

xanatos
xanatos

Reputation: 111860

You can

encoder.QRCodeScale = (number).

It controls the "scale" used to create the image.

The "scale" controls directly the size of the dots (in pixels). A scale of 1 means each dot is 1 pixel wide/high.

Note that there is a bug in the size calculation, so the image could be cropped of 1 pixel both in width and in height (if you look, the last row/column are blank)

QRCodeEncoder encoder = new QRCodeEncoder();
encoder.QRCodeScale = 1;

using (var bmp = encoder.Encode(EmployeeId))
{
    // There is a bug in QRCodeEncoder, so that it will add an
    // additional "blank" row/column
    var bmp2 = bmp.Clone(new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1), bmp.PixelFormat))

    // use bmp2 
}

Upvotes: 4

Related Questions