查看: 1747|回复: 0

[.NET开发] C#生成二维码

发表于 2018-3-6 08:00:00

http://zxingnet.codeplex.com/

https://www.cnblogs.com/WhyShang/p/3704513.html

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. string msg = textBox1.Text;
  4. Image img = GenByZXingNet(msg);
  5. this.pictureBox1.Image=img;
  6. }
  7. /// <summary>
  8. /// 生成二维码图片
  9. /// </summary>
  10. /// <param name="strMessage">要生成二维码的字符串</param>
  11. /// <param name="width">二维码图片宽度</param>
  12. /// <param name="height">二维码图片高度</param>
  13. /// <returns></returns>
  14. private Bitmap GenByZXingNet(string msg)
  15. {
  16. BarcodeWriter writer = new BarcodeWriter();
  17. writer.Format = BarcodeFormat.QR_CODE;
  18. writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
  19. writer.Options.Hints.Add(
  20. EncodeHintType.ERROR_CORRECTION,
  21. ZXing.QrCode.Internal.ErrorCorrectionLevel.H
  22. );
  23. const int codeSizeInPixels = 250; //设置图片长宽
  24. writer.Options.Height = writer.Options.Width = codeSizeInPixels;
  25. writer.Options.Margin = 0;//设置边框
  26. ZXing.Common.BitMatrix bm = writer.Encode(msg);
  27. Bitmap img = writer.Write(bm);
  28. return img;
  29. }
复制代码



回复

使用道具 举报