Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant I use Gma.QrCodeNet.Encoding.Windows.Render?

I'm using QrCode.Net library version 0.3 and I need to use Gma.QrCodeNet.Encoding.Windows.Render in order to create images with qrcode ISizeCalculation but I'm missing somethig or there's another version outhere. What can be the problem? Anyway I found a solution for people with the same problem and they wanna create images with the same fixed size. Here is the code:

private void gen_qr_file(string file_name, string content, int image_size)
{
    string new_file_name = file_name;

    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
    QrCode qrCode = new QrCode();
    qrEncoder.TryEncode(content, out qrCode);

    Renderer renderer = new Renderer(image_size, Brushes.Black, Brushes.White);

    MemoryStream ms = new MemoryStream();

    renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);

    var image = new Bitmap(Image.FromStream(ms), new Size(new Point(200, 200)));

    image.Save(new_file_name + ".png", ImageFormat.Png);

}

This generate a png image of 200x200 pixels with the qrcode.

The library itself has a method to do this, but I need to include the RENDER thing and I can't. Someone knows what's the problem?

like image 771
Alejandro del Río Avatar asked Feb 19 '23 03:02

Alejandro del Río


2 Answers

private void gen_qr_file(string file_name, string content, int image_size) {

    string new_file_name = file_name;

    QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
    QrCode qrCode = new QrCode();
    qrEncoder.TryEncode(content, out qrCode);

    Renderer renderer = new Renderer(image_size, Brushes.Black, Brushes.White);

    MemoryStream ms = new MemoryStream();

    renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.Png);

    var imageTemp = new Bitmap(ms);

    var image = new Bitmap(imageTemp, new Size(new Point(image_size, image_size)));

    image.Save(new_file_name + ".png", ImageFormat.Png);

}

Note: Only 2 lines are modified. I hope it helps somebody.

like image 199
AltosNet Avatar answered Feb 21 '23 18:02

AltosNet


Use FixedCodeSize. See example below which will produce a 400x400px image, with each 'module' (block) getting smaller the more data is added.

var qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
var qrCode = qrEncoder.Encode("my value");

var renderer = new GraphicsRenderer(new FixedCodeSize(400, QuietZoneModules.Zero), Brushes.Black, Brushes.White);
renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, /* OUTPUT STREAM */);
like image 32
simbolo Avatar answered Feb 21 '23 16:02

simbolo