最近发现很多网站都是使用的彩色二维码,放眼望去,网站档次明显提升
相比传统黑白二维码,彩色二维码总会给人眼前一亮的感觉
(如果你不认为彩色二维码比黑白二维码有趣,也可以不必拘泥于形式)
下面来看下C#.Net中如何生成彩色二维码。
简介:C#.Net使用ZXing帮助类生成彩色二维码,二维码色调、二维码大小根据自己需要可任意调节。
Html页面布局(其实就是一个图片标签用于展示生成的二维码)
<!DOCTYPE html> <html> <head runat="server"> <title>C#.Net生成彩色渐变二维码案例</title> </head> <body> <form id="form1" runat="server"> <div> <img src="/QcHandler.ashx?act=getqc" height="164" width="164"> </div> </form> </body> </html>C#后端业务逻辑部分代码展示
using System; using System.Collections.Generic; using System.Linq; using System.Web; using com.google.zxing.qrcode; using System.Collections; using com.google.zxing; using System.Drawing; using com.google.zxing.qrcode.decoder; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Text; using System.IO; namespace ZXingQcWeb { /// <summary> /// C#.Net生成彩色渐变二维码案例 /// 作者:www.jsons.cn /// </summary> public class QcHandler : IHttpHandler { delegate void func(HttpContext context); static Dictionary<string, func> services = new Dictionary<string, func>(); public void ProcessRequest(HttpContext context) { string action = context.Request["act"]; HttpContext.Current.Response.ContentType = "image/png"; if (!string.IsNullOrEmpty(action) && services.ContainsKey(action)) { services[action](context); } } static QcHandler() { //生成二维码 services.Add("getqc", delegate(HttpContext context) { try { string httpstr = "http://www.jsons.cn"; QRCodeWriter writer = new QRCodeWriter(); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.Add(EncodeHintType.VERSION_START, 5); Bitmap image = writer.encode(httpstr, BarcodeFormat.QR_CODE, 0x150, 0x150, hints).ToBitmap();//黑白二维码 Bitmap bitmap2 = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(bitmap2); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawImage(image, 0, 0); image.Dispose(); Bitmap bitmap3 = QrCodeVertical(bitmap2.Width, bitmap2.Height);//彩色渐变二维码 Color color = Color.FromArgb(200, 224, 120, 1); int num = 96; try { num -= (Encoding.UTF8.GetBytes(httpstr).Length - 20) / 2; } catch (Exception) { } int num2 = num; int num3 = num2; for (int i = 0; i < bitmap2.Width; i ) { for (int j = 0; j < bitmap2.Height; j ) { Color color3; Color pixel = bitmap2.GetPixel(i, j); if ((i < num2) && (j < num3)) { color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? color : pixel; } else { color3 = ((pixel.A == 0xff) && (pixel.B == 0)) ? bitmap3.GetPixel(i, j) : pixel; } bitmap2.SetPixel(i, j, color3); } } bitmap3.Dispose(); MemoryStream ms = new MemoryStream(); bitmap2.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Close(); context.Response.BinaryWrite(ms.ToArray()); return; } catch { } }); } /// <summary> /// 设置二维码图片颜色 /// </summary> /// <param name="width"></param> /// <param name="heigth"></param> /// <returns></returns> private static Bitmap QrCodeVertical(int width, int heigth) { var image = new Bitmap(width, heigth, PixelFormat.Format32bppArgb); var rect = new Rectangle(0, 0, width, heigth); var brush = new LinearGradientBrush(rect, Color.FromArgb(230, 0x23, 0xa9, 160), Color.FromArgb(0xff, 8, 60, 0x63), LinearGradientMode.Vertical); Graphics graphics = Graphics.FromImage(image); graphics.FillRectangle(brush, rect); graphics.Dispose(); return image; } public bool IsReusable { get { return false; } } } }生成的彩色二维码效果展示
如果你有兴趣研究的话,可以下载此DEMO源码自行开发改造属于你自己的五彩二维码(注意:项目中已引用zxing.dll)。