/// <summary> /// 生成二维码,返回位图 /// </summary> /// <param name="input">输入的需要编码的信息字符串</param> /// <param name="fromat">二维码格式</param> /// <param name="CanvasWidth">画布宽度</param> /// <param name="CanvasHeight">画布高度</param> /// <param name="imageFormat">图像格式</param> /// <param name="logoImageFileName">中间嵌入的 Logo 图片</param> /// <returns></returns> public static Bitmap CreateCode(string input, BarcodeFormat fromat, int CanvasWidth, int CanvasHeight, System.Drawing.Imaging.ImageFormat imageFormat, string logoImageFileName) { System.Collections.Hashtable hints = new System.Collections.Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "utf-8"); ByteMatrix byteMatrix = new MultiFormatWriter().encode(input, fromat, CanvasWidth, CanvasHeight, hints); int x, y; int width = byteMatrix.Width; int height = byteMatrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (x = 0; x < width; x++) { for (y = 0; y < height; y++) { bmap.SetPixel(x, y, byteMatrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF")); } } if (String.IsNullOrEmpty(logoImageFileName.Trim()) || (!System.IO.File.Exists(logoImageFileName))) { return(bmap); } // 加图片水印 System.Drawing.Image logo = System.Drawing.Image.FromFile(logoImageFileName); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmap); // 设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; // 设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 清空画布并以透明背景色填充 //g.Clear(System.Drawing.Color.Transparent); // 计算 Logo 的范围 x = (width - logo.Width) / 2; y = (height - logo.Height) / 2; //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(logo, new System.Drawing.Rectangle(x, y, logo.Width, logo.Height), 0, 0, logo.Width, logo.Height, System.Drawing.GraphicsUnit.Pixel); logo.Dispose(); g.Dispose(); return(bmap); }
public static Bitmap Encode(string content, int width = 350, int height = 350) { com.google.zxing.common.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height); int matrixWidth = byteMatrix.Width; int matrixHeight = byteMatrix.Height; Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmap.SetPixel(x, y, byteMatrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF")); } } return(bmap); }
private Bitmap qrcode(string content, int width, int height) { Color fgColor = System.Drawing.Color.Black; //前景色 Color bgColor = Color.White; //背景色 Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //容错能力 hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); //字符集 ByteMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); Bitmap bmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? fgColor : bgColor); } } return(bmap); }