/// <summary> /// 生成带Logo二维码 /// </summary> /// <param name="content">内容</param> /// <param name="iconPath">logo路径</param> /// <param name="moduleSize">二维码的大小</param> /// <returns>输出流</returns> public static MemoryStream GetQRCode(string content, string iconPath, int moduleSize = 9) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例 //PS:追加的图片过大超过二维码的容错率会导致信息丢失,无法被识别 System.Drawing.Image img = System.Drawing.Image.FromFile(iconPath); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); MemoryStream memoryStream = new MemoryStream(); map.Save(memoryStream, ImageFormat.Jpeg); return(memoryStream); //生成图片的代码: map.Save(fileName, ImageFormat.Jpeg);//fileName为存放的图片路径 }
Bitmap ComputeQRCode(int slice) { var qrEncoder = new QrEncoder(FErrorCorrectionLevel[slice]); var qrCode = new QrCode(); if (qrEncoder.TryEncode(FText[slice], out qrCode)) { using (var fore = new SolidBrush(FForeColor[slice].Color)) using (var back = new SolidBrush(FBackColor[slice].Color)) { var renderer = new GraphicsRenderer(new FixedModuleSize(FPixelSize[slice], FQuietZoneModules[slice]), fore, back); DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width); var bmp = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); using (var g = Graphics.FromImage(bmp)) renderer.Draw(g, qrCode.Matrix); return(bmp); } } else { return(null); } }
public static byte[] Encode(string str) { var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); var qrCode = qrEncoder.Encode(str); var render = new GraphicsRenderer(new FixedModuleSize(3, QuietZoneModules.Zero)); var ms = new MemoryStream(); //render.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); int logosize = 20; using (Image img = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"Images\innerWhite-logo.png")) { Point imgPoint = new Point((map.Width - logosize) / 2, (map.Height - logosize) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, logosize, logosize); map.Save(ms, ImageFormat.Png); } return(ms.GetBuffer()); }
public static MemoryStream GenQrCode(string content, int size, Image logo) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L); QrCode qrCode; qrEncoder.TryEncode(content, out qrCode); GraphicsRenderer render = new GraphicsRenderer(new FixedCodeSize(size, QuietZoneModules.Two), Brushes.Black, Brushes.Transparent); MemoryStream ms = new MemoryStream(); Bitmap bitmap = new Bitmap(size, size); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(System.Drawing.Color.White); render.Draw(graphics, qrCode.Matrix); //计算画框的宽度高度以及位置 float rectWidth = logo.Width; float rectHeight = logo.Height; float rectX = (size - logo.Width) / 2; float rectY = (size - logo.Height) / 2; //加上水印 graphics.DrawImage(logo, rectX, rectY, rectWidth, rectHeight); bitmap.Save(ms, ImageFormat.Jpeg); return(ms); }
//生成中文二维码-支持中文(自定义大小) //暂时未完善 只定义了背景大小 static void Generate(string url, string filename, string savapath, ImgType type = ImgType.Png, int?width = null, int?high = null) { QrEncoder qrEncoder = new QrEncoder(); var qrCode = qrEncoder.Encode(url); //保存成png文件 //string filename = @"H:\桌面\截图\cn.png"; string suffix = ImgSuffix.Getsuffix(type);; //后缀 filename = $"{savapath}{filename}{suffix}"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); int w = 500; int h = 500; if (width != null) { w = (int)width; } if (high != null) { h = (int)high; } Bitmap map = new Bitmap(w, h); Graphics g = Graphics.FromImage(map); g.FillRectangle(Brushes.White, 0, 0, w, h); render.Draw(g, qrCode.Matrix, new Point(20, 20)); map.Save(filename, ImageFormat.Png); }
public byte[] GetGetQrCode(string content) { try { using (MemoryStream imgStream = new MemoryStream()) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qr; int ModuleSize = 12; //大小 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 if (qrEncoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵 { //Brush b = new SolidBrush(Color.FromArgb(20, Color.Gray)); var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones), Brushes.Black, Brushes.Transparent); //render.WriteToStream(qr.Matrix, ImageFormat.Png, imgStream); //logo DrawingSize dSize = render.SizeCalculator.GetSize(qr.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qr.Matrix); //logo //Image img = resizeImage(Image.FromFile(@"D:\code\learn\asp.net\0-part\webapi\WebApiTest\webapi1\Images\logo1.png"), new Size(100, 100)); //img.Save(@"D:\code\learn\asp.net\0-part\webapi\WebApiTest\webapi1\Images\qrlogo.png", ImageFormat.Png); //Image img = Image.FromFile(@"D:\code\Projects\速星创\server\SXC\SXC.WebApi\Images\qrlogo.png"); Image img = Image.FromFile(Function.GetImagePath("qrlogo.png")); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(imgStream, ImageFormat.Png); byte[] imgByte = imgStream.GetBuffer(); img.Dispose(); g.Dispose(); map.Dispose(); return(imgByte); } else { throw new Exception("二维码生成失败"); } } } catch (Exception ex) { throw ex; } }
protected override void Draw(GameTime gameTime) { _totalFrames++; GameWorld.TotalDrawTimer.Start(); GraphicsRenderer.Draw(); GameWorld.TotalDrawTimer.Measure(); base.Draw(gameTime); }
/// <summary> /// 开始绘制(仅覆盖) /// </summary> /// <param name="text">内容文字</param> /// <param name="pictureSize">图片大小</param> public void Draw(string text, int pictureSize) { QrCode qrCode = qrEncoder.Encode(text); FixedModuleSize moduleSize = new FixedModuleSize(pictureSize, QuietZoneModules.Zero); GraphicsRenderer render = new GraphicsRenderer(moduleSize, darkBrush, lightBrush); render.Draw(graphics, qrCode.Matrix); }
/// <summary> /// 根据文字内容等信息构建相应QRCode格式二维码图片 /// </summary> /// <param name="content">二维码内容信息</param> /// <param name="level">纠错级别</param> /// <param name="width">图片宽度,如小于0则使用默认尺寸</param> /// <param name="height">图片高度,如小于0则使用默认尺寸</param> /// <param name="inline">内嵌图片,如为Null则不做内嵌</param> /// <returns>二维码图片</returns> public static Image BuildQRCode(String content, ErrorCorrectionLevel level, int width, int height, Image inline) { QrEncoder encoder = new QrEncoder(level); QrCode code = null; if (!encoder.TryEncode(content, out code)) { return(null); } GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(ModuleSizeInPixels, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize cSize = render.SizeCalculator.GetSize(code.Matrix.Width); Size oSize = new Size(cSize.CodeWidth, cSize.CodeWidth) + new Size(2 * PaddingSizeInPixels, 2 * PaddingSizeInPixels); Image img = new Bitmap(oSize.Width, oSize.Height); using (Graphics g = Graphics.FromImage(img)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; render.Draw(g, code.Matrix); if (inline != null) { int iw = (int)(oSize.Width * InlineSizeInProportion); int ih = (int)(oSize.Height * InlineSizeInProportion); int il = (oSize.Width - iw) / 2; int it = (oSize.Height - ih) / 2; g.DrawImage(inline, it, il, iw, ih); Pen pen = new Pen(Color.White, 1); using (GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(it - 1, il - 1, iw + 1, ih + 1), 4)) { g.DrawPath(pen, path); } } } if (width > 0 && height > 0) { int w = width > 0 ? width : code.Matrix.Width; int h = height > 0 ? height : code.Matrix.Height; Image imgCode = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(imgCode)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(img, 0, 0, width, height); } img.Dispose(); img = imgCode; } return(img); }
private void ShowCode(Graphics g) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L); QrCode qrCode = qrEncoder.Encode(str); FixedModuleSize moduleSize = new FixedModuleSize(2, QuietZoneModules.Two); GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White); render.Draw(g, qrCode.Matrix); }
//[Route("api/XXQrCode/{content}")] //[HttpGet] public HttpResponseMessage GetQrCodeX(string content) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qr; int ModuleSize = 12; //大小 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 MemoryStream imgStream = new MemoryStream(); if (qrEncoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵 { //Brush b = new SolidBrush(Color.FromArgb(20, Color.Gray)); var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones), Brushes.Black, Brushes.Transparent); //render.WriteToStream(qr.Matrix, ImageFormat.Png, imgStream); //logo DrawingSize dSize = render.SizeCalculator.GetSize(qr.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qr.Matrix); //logo //Image img = resizeImage(Image.FromFile(@"D:\code\learn\asp.net\0-part\webapi\WebApiTest\webapi1\Images\logo1.png"), new Size(100, 100)); //img.Save(@"D:\code\learn\asp.net\0-part\webapi\WebApiTest\webapi1\Images\qrlogo.png", ImageFormat.Png); //Image img = Image.FromFile(@"D:\code\Projects\速星创\server\SXC\SXC.WebApi\Images\qrlogo.png"); Image img = Image.FromFile(@"D:\code\Projects\速星创\server\SXC\SXC.WebApi\Images\qrlogo.png"); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(imgStream, ImageFormat.Png); img.Dispose(); g.Dispose(); var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(imgStream.GetBuffer()) //或者 //Content = new StreamContent(imgStream) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); return(resp); } else { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
private void ShowCode(Graphics g) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L); QrCode qrCode = qrEncoder.Encode(str); FixedModuleSize moduleSize = new FixedModuleSize(4, QuietZoneModules.Two); GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White); Point mP = new Point(627, 595);//设置二维码左上角的位置 render.Draw(g, qrCode.Matrix, mP); }
public Image getqrcode(string content) { var encoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = encoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(12, QuietZoneModules.Two), Brushes.Black, Brushes.White);//如需改变二维码大小,调整12即可 DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); return(map); }
//生成中文二维码-支持中文 public static void Generate3() { QrEncoder qrEncoder = new QrEncoder(); QrCode qrCode = qrEncoder.Encode("我是小天马"); //保存成png文件 string filename = @"D:\09VS\08temp\cn.png"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); Bitmap map = new Bitmap(500, 500); Graphics g = Graphics.FromImage(map); g.FillRectangle(Brushes.Red, 0, 0, 500, 500); render.Draw(g, qrCode.Matrix, new Point(20, 20)); map.Save(filename, ImageFormat.Png); }
//这个方法保存的图像会太大,不好 private void button3_Click(object sender, EventArgs e) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode("我是abc小def天马\n@、"); //保存成png文件 string filename = @"QRCodeChinese.png"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); Bitmap map = new Bitmap(500, 500); Graphics g = Graphics.FromImage(map); g.FillRectangle(Brushes.Red, 0, 0, 500, 500); render.Draw(g, qrCode.Matrix, new Point(20, 20)); map.Save(filename, ImageFormat.Png); }
public void EncodeToPng(string str, string picpath, QRCodeSaveSize saveSize) { QrCode qrCode = _qrEncoder.Encode(str); //保存成png文件 GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(Convert.ToInt32(saveSize), QuietZoneModules.Two), Brushes.Black, Brushes.White); using (FileStream stream = new FileStream(picpath, FileMode.OpenOrCreate)) //OpenOrCreate打开新文件默认就是truncate的了,如果再显式指定Truncate反而会报错当文件不存在的时候 { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } Bitmap bitmap = new Bitmap(165, 165); Graphics g = Graphics.FromImage(bitmap); render.Draw(g, qrCode.Matrix); bitmap.Save("a.bmp"); }
/// <summary> /// 生成二维码的方法 /// </summary> /// <param name="str">二维码包含的内容</param> /// <param name="moduleSize">二维码大小</param> /// <param name="no">编号</param> private void createQrCode(string content, int no, int moduleSize = 9) { //QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); //QrCode qrCode = new QrCode(); //qrEncoder.TryEncode(str, out qrCode); string filename = DateTime.Now.ToString("d"); filename = filename.Replace("/", ""); filename = filename.Replace(":", ""); filename = filename.Replace("-", ""); filename = filename.Replace(".", ""); filename = filename.Replace(" ", ""); string filepath = System.AppDomain.CurrentDomain.BaseDirectory + filename; if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } string fileName = filepath + "\\" + no + ".png"; //Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White); //renderer.CreateImageFile(qrCode.Matrix, @filename, ImageFormat.Png); //ErrorCorrectionLevel 误差校正水平 //QuietZoneModules 空白区域 var encoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = encoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZoneModules.Two), Brushes.Black, Brushes.White); //MemoryStream memoryStream = new MemoryStream(); //render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, memoryStream); //return memoryStream; //生成图片的代码 DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); map.Save(fileName, ImageFormat.Png);//fileName为存放的图片路径 }
//生成QRCode的函数 public void DrawQRCode(string qrText) { //pictureBox1重新生成 pictureBox1.Refresh(); //新建QrEncoder类的对象 QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); //新建QrCode类的对象,传入形参 QrCode qrCode = qrEncoder.Encode(qrText); //新建GraphicsRenderer类的对象 GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); //确定绘图初始点 Point padding = new Point(0, 0); //实例化Graphics类 Graphics graphics = pictureBox1.CreateGraphics(); //将graphics对象导入pictureBox1 render.Draw(graphics, qrCode.Matrix, padding); }
public static MemoryStream GenQrCode(string content, int size) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L); QrCode qrCode; qrEncoder.TryEncode(content, out qrCode); GraphicsRenderer render = new GraphicsRenderer(new FixedCodeSize(size, QuietZoneModules.Zero), Brushes.Black, Brushes.Transparent); MemoryStream ms = new MemoryStream(); Bitmap bitmap = new Bitmap(size, size); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(System.Drawing.Color.White); render.Draw(graphics, qrCode.Matrix); bitmap.Save(ms, ImageFormat.Jpeg); return(ms); }
private void logoEncode(string filename, string source, string logoPath) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(source); //保存成png文件 GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例 Image img = Image.FromFile(logoPath); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(filename, ImageFormat.Png); }
/// <summary> /// 生成二维码 /// </summary> /// <param name="Content">内容文本</param> /// <param name="size">图片尺寸(像素),0表示不设置</param> /// <returns></returns> public string CreateQRCode(string Content, int Size) { Graphics g = null; Bitmap img = null; QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(Content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(Size, QuietZoneModules.Four), Brushes.Black, Brushes.White); Point padding = new Point(10, 10); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); img = new Bitmap(dSize.CodeWidth + padding.X, dSize.CodeWidth + padding.Y); //创建GDI绘图图面对象 g = Graphics.FromImage(img); render.Draw(g, qrCode.Matrix, padding); //创建文件夹 Common.CreateDir(filePath); //文件名称 string guid = Guid.NewGuid().ToString().Replace("-", "") + ".png"; RelativePath += guid; string QRCodePath = filePath + "/" + guid; try { using (FileStream stream = new FileStream(QRCodePath, FileMode.Create)) { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } } catch (Exception e) { string ServiceName = "SaveQRCode"; string LogBody = "---执行服务出现错误,异常信息:" + e.Message + ",时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---\r\n"; Common.WriteLogFile(Common.getErrorLogFile(ServiceName), LogBody); } finally { //释放占用资源 img.Dispose(); g.Dispose(); } return(RelativePath); }
/// <summary> /// Gets the QR code containing the data of the network component. /// </summary> /// <returns>The QR code image.</returns> public Bitmap GetQRCode( ) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); byte[] ipBytes = IP.GetAddressBytes( ); QrCode qrCode = qrEncoder.Encode($"{string.Join( ",", ipBytes )},{Port}"); const int moduleSizeInPixels = 25; GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(moduleSizeInPixels, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap bmp = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); using (Graphics graphics = Graphics.FromImage(bmp)) renderer.Draw(graphics, qrCode.Matrix); return(bmp); }
/// <summary> /// 二维码生成 /// </summary> /// <param name="content"></param> /// <param name="title"></param> /// <returns></returns> private System.Drawing.Image MakeTryQRCode(string content, string title) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(content); GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(15, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap bitmap = new Bitmap(dSize.CodeWidth + 10, dSize.CodeWidth + 114); using (Graphics g = Graphics.FromImage(bitmap)) { //背景色:白 g.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height); //描画二维码 render.Draw(g, qrCode.Matrix, new Point(10, 10)); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //描画中间的log string logFile = Path.Combine(Application.StartupPath, "logo.png"); if (File.Exists(logFile)) { Size size = new Size(160, 160); Point point = new Point(bitmap.Width / 2 - size.Width / 2, bitmap.Width / 2 - size.Height / 2); //描画背景 g.FillEllipse(Brushes.White, point.X, point.Y, size.Width, size.Height); //描画logo System.Drawing.Image img = Bitmap.FromFile(logFile); TextureBrush texture = new TextureBrush(img); texture.TranslateTransform(-75, -80); texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile; g.FillEllipse(texture, point.X + 8, point.Y + 8, size.Width - 16, size.Height - 16); } //描画下面的描述性文字 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Near; sf.FormatFlags = StringFormatFlags.NoWrap; g.DrawString(title, new Font("Simhei", 32f, FontStyle.Bold), Brushes.Black, new RectangleF(30, bitmap.Height - 100, bitmap.Width - 60, 100), sf); } return(bitmap); }
//生成带Logo的二维码 static void Generate5() { QrEncoder qrEncoder = new QrEncoder(); var qrCode = qrEncoder.Encode("我是小天马"); //保存成png文件 string filename = @"H:\桌面\截图\logo.png"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例 Image img = Image.FromFile(@"F:\JavaScript_Solution\QrCode\QrCode\Images\101.jpg"); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(filename, ImageFormat.Png); }
/// <summary> /// 生成带logo的二维码 /// </summary> public static void GenerateLogoQr() { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode("http://www.yunfangdata.com"); //保存成png文件 string filename = System.Environment.CurrentDirectory + "\\二维码.png"; //@"H:\桌面\截图\logo.png"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例 Image img = Image.FromFile(System.Environment.CurrentDirectory + "\\img\\logo2.png"); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(filename, ImageFormat.Png); }
public static void RunSample2() { const string helloWorld = "Hello World!"; QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = qrEncoder.Encode(helloWorld); const int moduleSizeInPixels = 5; GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(moduleSizeInPixels, QuietZoneModules.Two), Brushes.Black, Brushes.White); Panel panel = new Panel(); Point padding = new Point(10, 16); DrawingSize dSize = renderer.SizeCalculator.GetSize(qrCode.Matrix.Width); panel.AutoSize = false; panel.Size = new Size(dSize.CodeWidth, dSize.CodeWidth) + new Size(2 * padding.X, 2 * padding.Y); using (Graphics graphics = panel.CreateGraphics()) { renderer.Draw(graphics, qrCode.Matrix, padding); } }
public static Bitmap GenerateQRCode(string text) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = qrEncoder.Encode(text); //ModuleSize 设置图片大小 //QuietZoneModules 设置周边padding /* * 5----150*150 padding:5 * 10----300*300 padding:10 */ GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(10, QuietZoneModules.Two), Brushes.Black, Brushes.White); Point padding = new Point(10, 10); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth + padding.X, dSize.CodeWidth + padding.Y); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix, padding); return(map); }
private Bitmap GetQRCodeBitmap(string address) { var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); var qrCode = new QrCode(); var isSuccess = qrEncoder.TryEncode(address, out qrCode); if (!isSuccess) { Commons.ShowInfoBox(this, "生成二维码失败"); return(null); } var render = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Zero), Brushes.Black, Brushes.White); var map = new Bitmap(qrCode.Matrix.Width * 5, qrCode.Matrix.Height * 5); var g = Graphics.FromImage(map); g.FillRectangle(Brushes.White, 0, 0, qrCode.Matrix.Width * 5, qrCode.Matrix.Height * 5); render.Draw(g, qrCode.Matrix); pictureBox1.Location = new Point(panel1.Width / 2 + panel1.Location.X / 2 - map.Width / 2, panel1.Height / 2 - map.Height / 2); if (tbLogo.Text.Trim() != "") { Image logo = null; try { logo = Image.FromFile(tbLogo.Text.Trim()); if (logo.Width > 100 || logo.Height > 100) { throw (new System.OutOfMemoryException()); } var logoPoint = new Point((map.Width - logo.Width) / 2, (map.Height - logo.Height) / 2); g.DrawImage(logo, logoPoint.X, logoPoint.Y, logo.Width, logo.Height); } catch { Commons.ShowInfoBox(this, "无法使用此Logo"); } } return(map); }
//生成带Logo的二维码 public static void Generate5() { QrEncoder qrEncoder = new QrEncoder(); QrCode qrCode = qrEncoder.Encode("我是小天马"); //保存成png文件 string filename = @"D:\09VS\08temp\logo.png"; GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(35, QuietZoneModules.Four), Brushes.Black, Brushes.White); DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width); Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth); Graphics g = Graphics.FromImage(map); render.Draw(g, qrCode.Matrix); //追加Logo图片 ,注意控制Logo图片大小和二维码大小的比例 Image img = Image.FromFile(@"D:\09VS\08temp\Images\101.jpg"); System.Diagnostics.Debug.Print(map.Width + "-- " + map.Height); System.Diagnostics.Debug.Print(img.Width + "//" + img.Height); Point imgPoint = new Point((map.Width - img.Width) / 2, (map.Height - img.Height) / 2); g.DrawImage(img, imgPoint.X, imgPoint.Y, img.Width, img.Height); map.Save(filename, ImageFormat.Png); }
private void PaintQR(string str) { var graphics = this.CreateGraphics(); graphics.Clear(Color.White); if (string.IsNullOrEmpty(str)) { graphics.DrawString("Clipboard text is empty.", SystemFonts.DefaultFont, Brushes.Black, graphics.VisibleClipBounds); return; } try { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.L); QrCode qrCode = qrEncoder.Encode(str); var moduleSize = new FixedCodeSize(Math.Min((int)graphics.VisibleClipBounds.Width, (int)graphics.VisibleClipBounds.Height), QuietZoneModules.Two); GraphicsRenderer render = new GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White); render.Draw(graphics, qrCode.Matrix); } catch (Exception e) { graphics.DrawString(e.Message, SystemFonts.DefaultFont, Brushes.Black, graphics.VisibleClipBounds); } }