C#利用QrCode.Net生成(Qr码)二维码

输入字符串长度:74个

EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221

同时,QrCode.Net可以对字符串进行分析,决定是否使用UTF-8编码。(比如使用中文的时候。)

QrCode使用方法:

新建项目添加对类库的引用,然后引入Gma.QrCodeNet.Encoding命名空间。

using Gma.QrCodeNet.Encoding;

在控制台中输出二维码:

Console.Write(@"Type some text to QR code: ");

string sampleText = Console.ReadLine();

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);

QrCode qrCode = qrEncoder.Encode(sampleText);

for (int j = 0; j < qrCode.Matrix.Width; j++)

{

    for (int i = 0; i < qrCode.Matrix.Width; i++)

    {

 

        char charToPrint = qrCode.Matrix[i, j] ? '█' ' ';

        Console.Write(charToPrint);

    }

    Console.WriteLine();

}

Console.WriteLine(@"Press any key to quit.");

Console.ReadKey();

在Graphics上绘制二维码:

      const string helloWorld = "Hello World!";

 

           QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);

           QrCode qrCode = qrEncoder.Encode(helloWorld);

 

           const int moduleSizeInPixels = 5;

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

 

           Panel panel = new Panel();

           Point padding =  new Point(10,16);

           Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);

           panel.AutoSize = false;

           panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);

             

           using (Graphics graphics = panel.CreateGraphics())

           {

               renderer.Draw(graphics, qrCode.Matrix, padding);

           }

在WriteableBitmap上绘制二维码: 

const string helloWorld = "Hello World!";

  

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);

QrCode qrCode = new QrCode();

qrEncoder.TryEncode(helloWorld, out qrCode);

  

const int moduleSizeInPixels = 5;

Renderer renderer = new Renderer(moduleSizeInPixels);   //Black&White is default colour for drawing QrCode

  

//Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width. 

int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);

  

WriteableBitmap wBitmap = new WriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);

  

//If wBitmap is null value. renderer will create Gray8 Bitmap as default.

renderer.Draw(wBitmap, qrCode.Matrix);    //Default offset position is (0, 0);

  

//Now you can put wBitmap to Image control's Source or use it to create image file.

如果需要把二维码呈现在WinForm或者WPF应用程序中,可以直接把类库拖入工具箱,然后直接在窗体上拖出控件。

 

直接把二维码保存到文件:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);

QrCode qrCode = new QrCode();

qrEncoder.TryEncode(helloWorld, out qrCode);

 

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

renderer.CreateImageFile(qrCode.Matrix, @"c:\temp\HelloWorld.png", ImageFormat.Png);

将二维码写入Stream:

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);

QrCode qrCode = new QrCode();

qrEncoder.TryEncode(helloWorld, out qrCode);

 

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

MemoryStream ms = new MemoryStream();

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值