QRUtils.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. using QRCoder;
  2. using ShareWifi.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ShareWifi.Utils
  11. {
  12. class QRUtils
  13. {
  14. /// <summary>
  15. /// 根据 data 生成二维码
  16. /// </summary>
  17. /// <param name="data"></param>
  18. public static QRModel genQR(string data)
  19. {
  20. QRCodeGenerator qrGenerator = new QRCodeGenerator();
  21. QRCodeData qrCodeData = qrGenerator.CreateQrCode(data, QRCodeGenerator.ECCLevel.Q);
  22. QRCode qrCode = new QRCode(qrCodeData);
  23. Bitmap bitmap = qrCode.GetGraphic(5);
  24. //保存图片
  25. string fileName = System.DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss") + ".jpg";
  26. string filePath = System.Environment.CurrentDirectory;
  27. //bitmap.Save(filePath + "/" + fileName, ImageFormat.Jpeg);
  28. return new QRModel(filePath, fileName, bitmap);
  29. }
  30. }
  31. }