最近發(fā)現(xiàn)很多網(wǎng)站都是使用的彩色二維碼,放眼望去,網(wǎng)站檔次明顯提升
相比傳統(tǒng)黑白二維碼,彩色二維碼總會(huì)給人眼前一亮的感覺
(如果你不認(rèn)為彩色二維碼比黑白二維碼有趣,也可以不必拘泥于形式)
下面來看下C#.Net中如何生成彩色二維碼。
簡介:C#.Net使用ZXing幫助類生成彩色二維碼,二維碼色調(diào)、二維碼大小根據(jù)自己需要可任意調(diào)節(jié)。
Html頁面布局(其實(shí)就是一個(gè)圖片標(biāo)簽用于展示生成的二維碼)
<!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#后端業(yè)務(wù)邏輯部分代碼展示
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生成彩色漸變二維碼案例 /// 作者:hnxxbl.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://hnxxbl.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> /// 設(shè)置二維碼圖片顏色 /// </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源碼自行開發(fā)改造屬于你自己的五彩二維碼(注意:項(xiàng)目中已引用zxing.dll)。