/// <summary> /// 获取二维码 /// </summary> /// <param name="strContent">待编码的字符</param> /// <param name="ms">输出流</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> public static bool GetQRCode(string strContent, MemoryStream ms) { try { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = strContent; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } else { return(false); } return(true); } catch { return(false); } }
/// 获取二维码 /// </summary> /// <param name="content">内容</param> /// <param name="ModuleSize">大小(数值*25px)</param> /// <returns>二维码图片</returns> public Bitmap GetQRCode(string content, int ModuleSize) { Bitmap bitmap = null; try { //生成二维码 using (MemoryStream ms = new MemoryStream()) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = content; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } bitmap = new Bitmap(ms); } } catch (Exception e) { throw e; } return(bitmap); }
/// <summary> /// 获取二维码 /// </summary> /// <param name="strContent">待编码的字符</param> /// <param name="ms">输出流</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> public static bool GetQRCode(string strContent, MemoryStream ms) { try { //误差校正水平 ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //待编码内容 string Content = strContent; //空白区域 QuietZoneModules QuietZones = QuietZoneModules.Two; //大小 int ModuleSize = 7; var encoder = new QrEncoder(Ecl); QrCode qr; //对内容进行编码,并保存生成的矩阵 if (encoder.TryEncode(Content, out qr)) { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } else { return(false); } } catch (Exception ex) { LogHelper.Error(ex); } finally { } return(true); }
/// <summary> /// 获取二维码 /// </summary> /// <param name="strContent">待编码的字符</param> /// <param name="ms">输出流</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> public static bool GetQRCode(string strContent, string savePath) { var ms = new MemoryStream(); ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = strContent; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { try { using (var stream = new FileStream(savePath, FileMode.Create)) { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream); } } catch (Exception ex) { return(false); } } else { return(false); } return(true); }
/// <summary> /// 创建二维码并保存到指定文件中 /// </summary> /// <param name="text">待编码字符串</param> /// <param name="size">二维码大小</param> /// <param name="fileName">输出路径(D:\123.png)</param> /// <returns>返回是否成功</returns> public static bool Save(string text, int size, string fileName) { // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 ErrorCorrectionLevel el = ErrorCorrectionLevel.H; //空白区域 有zreo 也就是0 没有边框 此处还要乘以2才得到空白区域的宽度。 QuietZoneModules qzm = QuietZoneModules.Two; //二维码大小 FixedModuleSize fms = new FixedModuleSize(size, qzm); QrEncoder qrEncoder = new QrEncoder(el); QrCode qrCode = null; try { if (qrEncoder.TryEncode(text, out qrCode))//对内容进行编码,并保存生成的矩阵 { GraphicsRenderer render = new GraphicsRenderer(fms, Brushes.Black, Brushes.White); //.WriteToStream(matrix, ImageFormat.Png, stream, new Point(600, 600)); 是跟打印有关的DPI分辨率的参数,默认即可,调整对调整图片大小没有作用 using (FileStream stream = new FileStream(fileName, FileMode.Create)) { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, stream); } } else { return(false); } } catch { return(false); } return(true); }
public DrawingSize(int moduleSize, int codeWidth, QuietZoneModules quietZoneModules) : this() { ModuleSize = moduleSize; CodeWidth = codeWidth; this.QuietZoneModules = quietZoneModules; }
public bool GetQRCode(string strContent, string path, string backgroundPath, int width, int height, int x, int y, MemoryStream ms) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = strContent; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); Bitmap b = new Bitmap(width, height); Graphics g = Graphics.FromImage(b); using (FileStream fs = new FileStream(backgroundPath, FileMode.Open)) { Bitmap bmp = new Bitmap(fs); Bitmap bmp2 = new Bitmap(ms); // Graphics g2 = Graphics.FromImage(bmp); g.DrawImage(bmp2, new Rectangle(x, y, width, height)); bmp.Save(path); } } else { return(false); } return(true); }
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; } }
//[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)); } }
/// <summary> /// Parse QueryString that define the QR code properties /// </summary> /// <param name="request">HttpRequest containing HTTP GET data</param> /// <returns>A QR code descriptor object</returns> public static CodeDescriptor Init(ErrorCorrectionLevel level, string content, QuietZoneModules qzModules, int moduleSize) { var cp = new CodeDescriptor(); //// Error correction level cp.Ecl = level; //// Code content to encode cp.Content = content; //// Size of the quiet zone cp.QuietZones = qzModules; //// Module size cp.ModuleSize = moduleSize; return cp; }
private static bool GetQRCode(string strContent, MemoryStream ms) { QrCode code; string str = strContent; QuietZoneModules modules = QuietZoneModules.Four; int num = 12; if (new QrEncoder(0).TryEncode(str, out code)) { new GraphicsRenderer(new FixedModuleSize(num, modules)).WriteToStream(code.Matrix, ImageFormat.Png, ms); return(true); } return(false); }
/// <summary> /// 生成二维码 /// </summary> /// <param name="qrcontent">生成二维码的内容</param> /// <param name="darkbrush">二维码线条的颜色</param> /// <param name="lightbrush">二维码线条空白的颜色</param> /// <param name="modulesize">二维码图片的大小(1=41px)</param> /// <param name="quietzonemodules">二维码外围空白</param> /// <returns>二维码字节数组</returns> public static byte[] Renderer( string content, Brush darkbrush, Brush lightbrush, int modulesize = 8, QuietZoneModules quietzonemodules = QuietZoneModules.Two, Bitmap logo = null) { //创建二维码生成类,ErrorCorrectionLevel为容错级别 QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode; //生成二维码 encoder.TryEncode(content, out qrCode); //FixedModuleSize参数1:二维码黑白线条的宽度 //FixedModuleSize参数2,QuietZoneModules:二维码外围空白 GraphicsRenderer render = new GraphicsRenderer( new FixedModuleSize(modulesize, QuietZoneModules.Zero), darkbrush, lightbrush); //将二维码写入到内存中 byte[] byteQrCode = new byte[] { }; using (MemoryStream ms = new MemoryStream()) { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms); if (logo != null) { var qrCodeImage = new Bitmap((Image) new Bitmap(ms)); using (var g = Graphics.FromImage(qrCodeImage)) { //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); g.DrawImage(qrCodeImage, 0, 0, qrCodeImage.Width, qrCodeImage.Height); //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框 g.DrawImage(logo, qrCodeImage.Width / 2 - logo.Width / 2, qrCodeImage.Width / 2 - logo.Width / 2, logo.Width, logo.Height); GC.Collect(); using (MemoryStream msNew = new MemoryStream()) { qrCodeImage.Save(msNew, ImageFormat.Png); byteQrCode = msNew.ToArray(); } } } else { byteQrCode = ms.ToArray(); } } return(byteQrCode); }
/// <summary> /// 生成二维码 /// </summary> /// <param name="content">内容</param> /// <param name="ms">写入的文件流</param> /// <returns></returns> public static bool General(string content, MemoryStream ms) { QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M); QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 QrCode code; if (encoder.TryEncode(content, out code)) { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(code.Matrix, ImageFormat.Jpeg, ms); return(true); } return(false); }
/// <summary> /// 根据url生成二维码 /// </summary> /// <param name="str"></param> /// <returns></returns> public string StringToImagePath(string str) { QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(str, out qrCode); int moduleSize = 6; QuietZoneModules quiet = QuietZoneModules.Two; var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, quiet)); using (FileStream file = File.OpenWrite(str)) { render.WriteToStream(qrCode.Matrix, ImageFormat.Png, file); } return(Path.GetFullPath("1.png")); }
private bool GenCode(string content, out MemoryStream ms) { ms = new MemoryStream(); ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M;//误差水平 QuietZoneModules qzm = QuietZoneModules.Two; int moduleSize = 12; QrEncoder encoder = new QrEncoder(ecl); QrCode qr; if (encoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, qzm)); render.WriteToStream(qr.Matrix, ImageFormat.Jpeg, ms); return(true); } return(false); }
public static MemoryStream generate_QR_Code(string url) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 //string Content = strContent;//待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; MemoryStream ms = new MemoryStream(); if (encoder.TryEncode(url, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } ms.Close(); return(ms); }
Bitmap encodeToBitMap(string data, double dimension_inches, float dpi_x = 600, float dpi_y = 600, ErrorCorrectionLevel correction_level = ErrorCorrectionLevel.L, QuietZoneModules quite_zone = QuietZoneModules.Zero ) { QrEncoder qrEncoder = new QrEncoder(correction_level); QrCode qrCode = qrEncoder.Encode(data); // Calculate number of pixels. Note we use dpi in x direction // but we should probably use whichever is lowest int pixels = (int)(dimension_inches * dpi_x); // Check whether we have enough space //if (pixels < qrCode.Matrix.Width) //throw new Exception("Too small"); ISizeCalculation iSizeCal = new FixedCodeSize(pixels, quite_zone); DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(iSizeCal, System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.White); //DrawingBrushRenderer dRenderer = new DrawingBrushRenderer(iSizeCal, System.Windows.Media.Brushes.Black, System.Windows.Media.Brushes.LightGray); MemoryStream mem_stream = new MemoryStream(); dRenderer.WriteToStream(qrCode.Matrix, ImageFormatEnum.BMP, mem_stream); Bitmap bitmap = new Bitmap(mem_stream); bitmap.SetResolution(dpi_x, dpi_y); // A different way to do the same. Just incase the bitmap.SetResolution function does not work //System.Windows.Point dpipoint = new System.Windows.Point(dpi, dpi); //BitmapSource bitmapsource = dRenderer.WriteToBitmapSource(qrCode.Matrix, dpipoint); //MemoryStream outStream = new MemoryStream(); //BitmapEncoder bitmapencoder = new BmpBitmapEncoder(); //BitmapFrame bitmapframe = BitmapFrame.Create(bitmapsource); //bitmapencoder.Frames.Add(bitmapframe); //bitmapencoder.Save(outStream); //Bitmap bitmap = new System.Drawing.Bitmap(outStream); return(bitmap); }
/// <summary> /// 获取二维码 /// </summary> /// <param name="strContent">待编码的字符</param> /// <param name="ms">输出流</param> /// <param name="ModuleSize">字体大小</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> public static bool GetQRCode(string strContent, MemoryStream ms, int ModuleSize = 12) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平,越高,二维码的有效像素点就越多 QuietZoneModules QuietZones = QuietZoneModules.Four; //空白区域 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(strContent, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } else { return(false); } return(true); }
/// <summary> /// 生成二维码流 /// </summary> /// <param name="qrcontent"></param> /// <returns></returns> public static MemoryStream GetQrCodeStream(string qrcontent) { //误差校正水平 ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.M; //空白区域 QuietZoneModules quietZone = QuietZoneModules.Zero; int ModuleSize = 120;//大小 QrCode qrCode; var encoder = new QrEncoder(ecLevel); //对内容进行编码,并保存生成的矩阵 if (encoder.TryEncode(qrcontent, out qrCode)) { var render = new GraphicsRenderer(new FixedCodeSize(ModuleSize, quietZone)); MemoryStream stream = new MemoryStream(); render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, stream); return(stream); } return(null); }
/// <summary> /// 获取二维码 /// </summary> /// <param name="content">待编码的字符</param> /// <param name="ms">输出流</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> public static bool GetQRCode(string content, MemoryStream ms) { ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M; //误差校正水平 QuietZoneModules quietZones = QuietZoneModules.Two; //空白区域 int moduleSize = 3; //大小 var encoder = new QrEncoder(ecl); QrCode qr; if (encoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, quietZones)); //render.WriteToStream(qr.Matrix, ImageFormat.Png, ms, new Point(72, 72)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);//默认96 } else { return(false); } return(true); }
void printDocument2_PrintPage(object sender, PrintPageEventArgs e) { int labels_per_page = 6; float spcae_between_labels = 0.0F; ErrorCorrectionLevel correction_level = _dic_error_correction[comboBoxCorrectionLevel.Text[0]]; QuietZoneModules quite_zone = _dic_quite_zone[comboBoxQuiteZone.Text[0]]; float x_offset = 0.0F; e.Graphics.PageUnit = GraphicsUnit.Pixel; for (int l = 0; l < labels_per_page; l++) { e.Graphics.DrawImage(_bitmaps_for_print[l], x_offset, 0.0F); //x_offset += (int)(label_width * 100); // Use when GraphicsUnit = Display x_offset += _bitmaps_for_print[l].Width + (spcae_between_labels * e.Graphics.DpiX); // Use when GraphicsUnit = Pixel } }
private bool CreateQR(string strcode, MemoryStream ms) { QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode code = new QrCode(); if (qrEncoder.TryEncode(strcode, out code)) { GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones), Brushes.Black, Brushes.White); render.WriteToStream(code.Matrix, ImageFormat.Png, ms); } else { return(false); } return(true); }
public static bool GetQRCode(string strContent, MemoryStream ms) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; string Content = strContent; var qz = QuietZoneModules.Four; QuietZoneModules QuietZones = QuietZoneModules.Two; int ModuleSize = 12; var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr)) { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); } else { return(false); } return(true); }
/// <summary> /// 使用Gma.QrCodeNet.Encoding 组件 /// </summary> /// <param name="content"></param> /// <returns></returns> public static byte[] GetQrCode(string content) { QrEncoder qr = new QrEncoder(); Gma.QrCodeNet.Encoding.QrCode code; //保存生成的二维码 QuietZoneModules QuietZones = QuietZoneModules.Zero; //空白区域 int ModuleSize = 10; //大小 MemoryStream ms = new MemoryStream(); if (qr.TryEncode(content, out code))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(code.Matrix, ImageFormat.Png, ms); //将二维码写入流 return(ms.ToArray()); //返回byte[] } return(null); }
/// <summary> /// 获取二维码 /// </summary> /// <param name="content">待编码的字符</param> /// <param name="ms">输出流</param> ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> private static bool GetQrCode(string content, MemoryStream ms) { const ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M; //误差校正水平 const QuietZoneModules quietZones = QuietZoneModules.Two; //空白区域 const int moduleSize = 12; //大小 QrCode qr; var encoder = new QrEncoder(ecl); if (encoder.TryEncode(content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, quietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); return(true); } return(false); }
/// <summary> /// 生成二维码流 /// </summary> /// <param name="qrcontent">二维码的内容</param> /// <param name="size">生成的二维码的尺寸。单位是像素</param> /// <returns>内存流。可保存为文件,或下载到客户端</returns> public static FileStreamInfo GetQrCodeStream(string qrcontent, int size) { //误差校正水平 ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.M; //空白区域 QuietZoneModules quietZone = QuietZoneModules.Zero; int ModuleSize = size;//大小 Gma.QrCodeNet.Encoding.QrCode qrCode; var encoder = new QrEncoder(ecLevel); //对内容进行编码,并保存生成的矩阵 if (encoder.TryEncode(qrcontent, out qrCode)) { var render = new GraphicsRenderer(new FixedCodeSize(ModuleSize, quietZone)); var stream = new FileStreamInfo(); render.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, stream); stream.FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; return(stream); } return(null); }
public bool GetQRCode(string strContent, string path, string backgroundPath, int width, int height, int x, int y, MemoryStream ms, string userFace, int centerImgSize) { ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = strContent; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones)); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); using (FileStream fs = new FileStream(backgroundPath, FileMode.Open)) { Bitmap bmp = new Bitmap(fs); Bitmap bmp2 = new Bitmap(ms); Graphics g2 = Graphics.FromImage(bmp); Bitmap bits = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(userFace); Bitmap icon = new Bitmap(bits, centerImgSize, centerImgSize); using (var graphics = System.Drawing.Graphics.FromImage(bmp2)) { // graphics.DrawImage(bits2, (bitmap.Width - bits2.Width) / 2, (bitmap.Height - bits2.Height) / 2); graphics.DrawImage(icon, (bmp2.Width - icon.Width) / 2, (bmp2.Height - icon.Height) / 2); } g2.DrawImage(bmp2, new Rectangle(x, y, width, height)); bmp.Save(path); } } else { return(false); } return(true); }
public HttpResponseMessage QRCode(string url) { MemoryStream ms = new MemoryStream(); int moduleSize = 12; //二维码大小 QuietZoneModules quietZones = QuietZoneModules.Two; //空白区域 ErrorCorrectionLevel ecl = ErrorCorrectionLevel.M; //误差校正水平 QrEncoder qrEncoder = new QrEncoder(ecl); QrCode qrCode = qrEncoder.Encode(url); var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, quietZones)); render.WriteToStream(qrCode.Matrix, System.Drawing.Imaging.ImageFormat.Png, ms); byte[] buffer = ms.GetBuffer(); string result = Convert.ToBase64String(buffer); StringWriter tw = new StringWriter(); JsonSerializer jsonSerializer = new JsonSerializer(); jsonSerializer.Serialize(tw, result, result.GetType()); return(new HttpResponseMessage { StatusCode = HttpStatusCode.Accepted, Content = new StringContent(tw.ToString(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json") }); }
private bool CreateQR(string strCode, string imgPath) { QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode code = new QrCode(); if (qrEncoder.TryEncode(strCode, out code)) { GraphicsRenderer render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones), Brushes.Black, Brushes.White); using (FileStream stream = new FileStream(imgPath, FileMode.Create)) { render.WriteToStream(code.Matrix, System.Drawing.Imaging.ImageFormat.Png, stream); } } else { return(false); } return(true); }
public Stream QrImage(string text) { var ms = new MemoryStream(); ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = text; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 12; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr)) //对内容进行编码,并保存生成的矩阵 { FixedModuleSize fms = new FixedModuleSize(ModuleSize, QuietZones); //Color.FromArgb(1, 127, 38, 0) Brush darkBrush = new SolidBrush(TranslateHexToRGBColor("C8000000")); //(Color.FromArgb(127, 38, 0)); Brush lightBrush = new SolidBrush(TranslateHexToRGBColor("00ffffff")); //Brushes.Transparent; var render = new GraphicsRenderer(fms, darkBrush, lightBrush); render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); return(ms); } else { return(null); } }
public ActionResult Save(ORDER_TASK_INFORMATION entity) { Common.ClientResult.OrderTaskGong result = new Common.ClientResult.OrderTaskGong(); try { Common.Account account = GetCurrentAccount(); if (string.IsNullOrWhiteSpace(entity.ID)) { List <COMPANY> companylist = m_BLL2.GetByParam(null, "asc", "ID", "COMPANYNAME&" + entity.INSPECTION_ENTERPRISE + ""); List <COMPANY> companylist2 = m_BLL2.GetByParam(null, "asc", "ID", "COMPANYNAME&" + entity.CERTIFICATE_ENTERPRISE + ""); foreach (var item in companylist) { if (item.COMPANY2 != null) { entity.INSPECTION_ENTERPRISEHELLD = item.COMPANY2.COMPANYNAME; break; } } foreach (var item in companylist2) { if (item.COMPANY2 != null) { entity.CERTIFICATE_ENTERPRISEHELLD = item.COMPANY2.COMPANYNAME; break; } else { entity.CERTIFICATE_ENTERPRISEHELLD = entity.CERTIFICATE_ENTERPRISE; break; } } string ORDER_NUMBER = m_BLL.GetORDER_NUMBER(ref validationErrors); var order = ORDER_NUMBER.Split('*');// DC2016001 * 1 * 2016 entity.ORDER_STATUS = Common.ORDER_STATUS_INFORMATION.保存.ToString(); var ms = new System.IO.MemoryStream(); entity.CREATETIME = DateTime.Now; entity.CREATEPERSON = account.PersonName; entity.ID = Result.GetNewId(); entity.ORDER_NUMBER = order[0].ToString(); entity.ORSERIALNUMBER = Convert.ToDecimal(order[1]); entity.ORYEARS = order[2].ToString(); entity.ORDER_STATUS = Common.ORDER_STATUS_INFORMATION.保存.ToString(); string path = Server.MapPath("~/up/ErWeiMa/"); foreach (var item in entity.APPLIANCE_DETAIL_INFORMATION) { item.ID = Result.GetNewId(); item.CREATETIME = DateTime.Now; item.CREATEPERSON = account.PersonName; string bianma = Regex.Replace(Guid.NewGuid().ToString().Replace("-", ""), "[a-z]", "", RegexOptions.IgnoreCase).Substring(0, 8); item.BAR_CODE_NUM = entity.ORSERIALNUMBER.ToString().PadLeft(4, '0') + bianma; //二维码生成 ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 string Content = item.ID; //待编码内容 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白区域 int ModuleSize = 3; //大小 var encoder = new QrEncoder(Ecl); QrCode qr; if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵 { Renderer r = new Renderer(ModuleSize); r.QuietZoneModules = QuietZones; r.WriteToStream(qr.Matrix, ms, ImageFormat.Png); } //QRCodeHelper.GetQRCode(item.ID, ms); var pathErWeiMa = path + item.ID + ".png"; //System.IO.FileStream fs = new System.IO.FileStream(pathErWeiMa, System.IO.FileMode.OpenOrCreate); //System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs); #region 二维码加字 System.IO.FileStream fss = new System.IO.FileStream(Server.MapPath("~/up/moban.png"), System.IO.FileMode.OpenOrCreate); int filelength = 0; filelength = (int)fss.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fss.Read(image, 0, filelength); //按字节流读取 System.Drawing.Image imag = System.Drawing.Image.FromStream(fss); //System.Drawing.Image Image = System.Drawing.Image.FromStream(ms); Graphics g = null; g = Graphics.FromImage(imag); string xinghao = item.BAR_CODE_NUM;//需要写入的字 //string xinghao = "123456789abcd";//需要写入的字 int w = imag.Width; int h = imag.Height; //entity.INSPECTION_ENTERPRISE StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; DrawString(g, item.BAR_CODE_NUM, new Font("宋体", 14), new SolidBrush(Color.Black), new PointF(430, 200), format, -90f); var COMPANY = m_BLL2.GetAll(); var dw = COMPANY.Where(m => m.COMPANYNAME == entity.INSPECTION_ENTERPRISE).Select(s => s.POSTCODE).Single(); DrawString(g, dw, new Font("宋体", 14), new SolidBrush(Color.Black), new PointF(490, 200), format, -90f); //g.DrawString(item.BAR_CODE_NUM, new Font("宋体", 10), Brushes.Red, new PointF(350, 0));//x:值越大越靠右;y:值越小越靠上 //if (entity.INSPECTION_ENTERPRISE.Length>9) //{ // string zhi = entity.INSPECTION_ENTERPRISE.Substring(0, 9); // string zhi2= entity.INSPECTION_ENTERPRISE.Remove(0, 9); // g.DrawString(zhi, new Font("宋体", 14), Brushes.Red, new PointF(0, 320));//x:值越大越靠右;y:值越小越靠上 // g.DrawString(zhi2, new Font("宋体", 14), Brushes.Red, new PointF(0, 380));//x:值越大越靠右;y:值越小越靠上 //} //else //{ // g.DrawString(entity.INSPECTION_ENTERPRISE, new Font("宋体", 14), Brushes.Red, new PointF(0, 320));//x:值越大越靠右;y:值越小越靠上 //} System.Drawing.Image ig = CombinImage(imag, ms); //System.Drawing.Image ig = imag; fss.Close(); TuPanBaoCun(ig, pathErWeiMa); //生成pdf //图片 //Image image = Image.GetInstance(imagePath); //cell = new PdfPCell(image, true); //table.AddCell(cell); PDF.Create(path + item.ID); #endregion //w.Write(ms.ToArray()); //fs.Close(); //器具明细信息_承接实验室表添加数据 foreach (var it in item.UNDERTAKE_LABORATORYID.TrimEnd(',').Split(',')) { item.APPLIANCE_LABORATORY.Add(new APPLIANCE_LABORATORY() { ID = Result.GetNewId(), UNDERTAKE_LABORATORYID = it, ORDER_STATUS = Common.ORDER_STATUS.保存.ToString(), EQUIPMENT_STATUS_VALUUMN = Common.ORDER_STATUS.保存.GetHashCode().ToString(), DISTRIBUTIONPERSON = account.PersonName, DISTRIBUTIONTIME = DateTime.Now, CREATEPERSON = account.PersonName, CREATETIME = DateTime.Now, ISRECEIVE = Common.ISRECEIVE.是.ToString(), RECYCLING = entity.RECYCLING }); } } ms.Close(); string returnValue = string.Empty; if (m_BLL.Create(ref validationErrors, entity)) { LogClassModels.WriteServiceLog(Suggestion.InsertSucceed + ",委托单信息的信息的Id为" + entity.ID, "委托单信息" );//写入日志 result.Code = Common.ClientCode.Succeed; result.Message = Suggestion.InsertSucceed; result.Id = entity.ID; return(Json(result)); //提示创建成功 } else { if (validationErrors != null && validationErrors.Count > 0) { validationErrors.All(a => { returnValue += a.ErrorMessage; return(true); }); } LogClassModels.WriteServiceLog(Suggestion.InsertFail + ",委托单信息的信息," + returnValue, "委托单信息" );//写入日志 result.Code = Common.ClientCode.Fail; result.Message = Suggestion.InsertFail + returnValue; return(Json(result)); //提示插入失败 } } else { } result.Code = Common.ClientCode.FindNull; result.Message = Suggestion.InsertFail + ",请核对输入的数据的格式"; //提示输入的数据的格式不对 } catch (Exception lastError) { // fss.Close(); ExceptionsHander.WriteExceptions(lastError);//将异常写入数据库 } return(Json(result)); }
/// <summary> /// FixedCodeSize is strategy for rendering QrCode at fixed Size. /// </summary> /// <param name="qrCodeWidth">Fixed size for QrCode pixel width. /// Pixel width have to be bigger than QrCode's matrix width(include quiet zone) /// QrCode matrix width is between 25 ~ 182(version 1 ~ 40).</param> public FixedCodeSize(int qrCodeWidth, QuietZoneModules quietZone) { m_QrCodeWidth = qrCodeWidth; m_QuietZoneModules = (int)quietZone; }
/// <summary> /// FixedModuleSize is strategy for rendering QrCode with fixed module pixel size. /// </summary> /// <param name="moduleSize">Size of the module.</param> /// <param name="quietZoneModules">The quiet zone modules.</param> /// <remarks></remarks> public FixedModuleSize(int moduleSize, QuietZoneModules quietZoneModules) { m_ModuleSize = moduleSize; m_QuietZoneModule = (int) quietZoneModules; }
public void SetQuietZoneModules(QuietZoneModules quietZoneModules) { SetValue(QuietZoneModuleProperty, quietZoneModules); }