使用QRCoder生成二维码
官方地址:https://github.com/codebude/QRCoder
首先安装相应的package
最简单的使用
public void GenerateRun(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); string filePath = "D:\\ImportData\\" + "\\QR-coder.jpg"; qrCodeImage.Save(filePath, ImageFormat.Jpeg); Console.WriteLine("generate finished"); }
设置颜色或是否周围是否有白边
public void GenerateRun(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); //Bitmap qrCodeImage = qrCode.GetGraphic(20); //drawQuietZones 是否有白边 Bitmap qrCodeImage = qrCode.GetGraphic(5, Color.Purple, Color.White, false); string filePath = "D:\\ImportData\\" + "\\QR-coder.jpg"; qrCodeImage.Save(filePath, ImageFormat.Jpeg); Console.WriteLine("generate finished"); }
设置logo
Bitmap qrCodeImage = qrCode.GetGraphic(20, Color.Black, Color.White, (Bitmap)Bitmap.FromFile("C:\\myimage.png"));
还有一些其它的支持功能
支持生成是WIFI数据的二维码
public void GenerateRun(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); //WIFI PayloadGenerator.WiFi wifiPayload = new PayloadGenerator.WiFi("MyWiFi-SSID", "MyWiFi-Pass", PayloadGenerator.WiFi.Authentication.WPA); content = wifiPayload.ToString(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(10); string filePath = "D:\\ImportData\\QR-coder.jpg"; qrCodeImage.Save(filePath, ImageFormat.Jpeg); Console.WriteLine("generate finished"); }
支持生成包含二维码的PDF
public void GeneratePDF(string content) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.Q); PdfByteQRCode qrCode = new PdfByteQRCode(qrCodeData); byte[] qrCodeAsPdfByteArr = qrCode.GetGraphic(20); string filename = "D:\\ImportData\\QR-coder.pdf"; FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write); fs.Write(qrCodeAsPdfByteArr, 0, qrCodeAsPdfByteArr.Length); fs.Close(); Console.WriteLine("generate pdf finished"); }
更多的格式内容参考:https://github.com/codebude/QRCoder#special-rendering-types