发布:2023/12/7 15:28:17作者:大数据 来源:大数据 浏览次数:509
nuget下载QRcoder和System.Drawing.Common两个插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public void QRcode() { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q); BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData); byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20); Bitmap bmp; using (var ms = new MemoryStream(qrCodeAsBitmapByteArr)) { bmp = new Bitmap(ms); //第一种 bmp.Save(Response.Body, ImageFormat.Jpeg); //第二种 //Response.Body.Write(ms.ToArray(), 0, (int)ms.Length); //Response.Body.Close(); } } public IActionResult QRcodeFile() { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q); BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData); byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20); Bitmap bmp; using (var ms = new MemoryStream(qrCodeAsBitmapByteArr)) { bmp = new Bitmap(ms); return File(ms.ToArray(), @"image/jpg"); } } |
上面注释的两行也可以使用,总结起来有两个方法,三种办法
两样都可以生成二维码图片,将图片返回到输出流
如想获取无边距二维码可以将上面的 BitmapByteQRCode代码替换如下:
1 2 3 4 5 6 7 |
QRCode qrCode1 = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode1.GetGraphic(20, Color.Black, Color.White, false); using (var ms = new MemoryStream()) { qrCodeImage.Save(ms, ImageFormat.Jpeg); return File(ms.ToArray(), @"image/jpg"); } |
https://github.com/codebude/QRCoder
GetGraphics方法具有更多的重载。前两个使您可以设置QR码图形的颜色。一种使用Color-class-types,另一种使用HTML十六进制颜色表示法。.
1 2 3 4 5 |
<span class="pl-c">//Set color by using Color-class types</span> <span class="pl-en">Bitmap</span> <span class="pl-smi">qrCodeImage</span> <span class="pl-k">=</span> <span class="pl-smi">qrCode</span>.<span class="pl-en">GetGraphic</span>(<span class="pl-c1">20</span>, <span class="pl-smi">Color</span>.<span class="pl-smi">DarkRed</span>, <span class="pl-smi">Color</span>.<span class="pl-smi">PaleGreen</span>, <span class="pl-c1">true</span>); <span class="pl-c">//Set color by using HTML hex color notation</span> <span class="pl-en">Bitmap</span> <span class="pl-smi">qrCodeImage</span> <span class="pl-k">=</span> <span class="pl-smi">qrCode</span>.<span class="pl-en">GetGraphic</span>(<span class="pl-c1">20</span>, <span class="pl-s"><span class="pl-pds">"</span>#000ff0<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>#0ff000<span class="pl-pds">"</span></span>); |
另一个重载使您可以在QR码的中心设置logo图像。.
1 |
<span class="pl-en">Bitmap</span> <span class="pl-smi">qrCodeImage</span> <span class="pl-k">=</span> <span class="pl-smi">qrCode</span>.<span class="pl-en">GetGraphic</span>(<span class="pl-c1">20</span>, <span class="pl-smi">Color</span>.<span class="pl-smi">Black</span>, <span class="pl-smi">Color</span>.<span class="pl-smi">White</span>, (<span class="pl-en">Bitmap</span>)<span class="pl-smi">Bitmap</span>.<span class="pl-en">FromFile</span>(<span class="pl-s"><span class="pl-pds">"</span>C:<span class="pl-cce">\\</span>myimage.png<span class="pl-pds">"</span></span>)); |
There are a plenty of other options. So feel free to read more on that in our wiki: Wiki: How to use QRCoder
winform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private void CreateQR(int pixelsPerModule, string info, Color qrColor, Color qrBackgroundColor, Bitmap logo, int iconSizePercent = 15, int iconBorderWidth = 6) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(info, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(pixelsPerModule, qrColor, qrBackgroundColor, logo, iconSizePercent, iconBorderWidth, true); picBoxQRCode.Image = qrCodeImage; } private void CreateQR(int pixelsPerModule, string info, Color qrColor, Color qrBackgroundColor) { QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(info, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(pixelsPerModule, qrColor, qrBackgroundColor, true); picBoxQRCode.Image = qrCodeImage; } |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4