public static System.Drawing.Bitmap Create(string content, System.Drawing.Image centralImage) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); System.Collections.Hashtable hashtable = new System.Collections.Hashtable(); hashtable.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hashtable.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); ByteMatrix byteMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, hashtable); System.Drawing.Bitmap bitmap = byteMatrix.ToBitmap(); System.Drawing.Size encodeSize = multiFormatWriter.GetEncodeSize(content, BarcodeFormat.QR_CODE, 300, 300); int num = System.Math.Min((int)((double)encodeSize.Width / 3.5), centralImage.Width); int num2 = System.Math.Min((int)((double)encodeSize.Height / 3.5), centralImage.Height); int x = (bitmap.Width - num) / 2; int y = (bitmap.Height - num2) / 2; System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap2)) { graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; graphics.DrawImage(bitmap, 0, 0); } System.Drawing.Graphics graphics2 = System.Drawing.Graphics.FromImage(bitmap2); graphics2.FillRectangle(System.Drawing.Brushes.White, x, y, num, num2); graphics2.DrawImage(centralImage, x, y, num, num2); return(bitmap2); }
//生成条形码 private void btnBarCode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); //if (content.Length != 13) //{ // MessageBox.Show("请输入13位的字符!"); // return; //} Regex regex = new Regex("^[0-9]{13}$"); if (!regex.IsMatch(content)) { MessageBox.Show("请输入13位的数字!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); ByteMatrix matrix = writer.encode(content, BarcodeFormat.EAN_13, 360, 150); Bitmap bitmap = matrix.ToBitmap(); pictureBox1.Image = bitmap; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//二维码 private void btnEncode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); if (String.IsNullOrEmpty(content)) { MessageBox.Show("请输入原文!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300); Bitmap bitmap = matrix.ToBitmap(); pictureBox1.Image = bitmap; //自动保存图片到当前目录 //string filename = Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; //bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); //labShow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static System.Drawing.Bitmap Create(string content) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); ByteMatrix byteMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300); return(byteMatrix.ToBitmap()); }
//带LOGO二维码 private void btnEncodePic_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); if (String.IsNullOrEmpty(content)) { MessageBox.Show("请输入编码内容"); return; } try { //构造二维码编码器 MultiFormatWriter writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(); hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q); //先生成二维码 ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300, hints); Bitmap img = matrix.ToBitmap(); //再处理要插入到二维码中的图片 Image middlImg = QRMiddleImg.Image; //获取二维码实际尺寸(去掉二维码两边空白后的尺寸) Size realSize = writer.GetEncodeSize(content, BarcodeFormat.QR_CODE, 300, 300); //计算插入的图片的大小和位置 int middleImgW = Math.Min((int)(realSize.Width / 4), middlImg.Width); int middleImgH = Math.Min((int)(realSize.Height / 4), middlImg.Height); int middleImgL = (img.Width - middleImgW) / 2; int middleImgT = (img.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //在二维码中插入图片 Graphics MyGraphic = Graphics.FromImage(bmpimg); //白底 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawString(content, this.Font, new SolidBrush(Color.Black), middleImgW * 2 + 20, 300 - middleImgH / 2); pictureBox1.Image = bmpimg; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static Bitmap Create(string content) { //构造二维码写码器 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(content, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); Bitmap img = bm.ToBitmap(); return(img); }
private void _SaleCupon_Initialize() { QRCodeWriter QRWriter = new QRCodeWriter(); ByteMatrix QRObject = QRWriter.encode("http://www.curfit.com/mobile/" + cuponhash.Data.hash, BarcodeFormat.QR_CODE, 600, 600); SaleCuponControl.imgQRCode.Source = getImageSource(QRObject.ToBitmap()); SaleCuponControl.btnClose.Click += new RoutedEventHandler(_SaleCupon_P_RightHandUp); SaleCuponControl.btnReturn.Click += new RoutedEventHandler(_SaleCupon_P_LeftHandUp); }
public static Bitmap createQRCode(String strCode, int widthAndHeight) { MultiFormatWriter mfwr = new MultiFormatWriter(); ByteMatrix matrix = mfwr.encode(strCode, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight); if (matrix != null) { Bitmap bitmap = matrix.ToBitmap(); return(bitmap); } return(null); }
/// <summary> /// What to do when the selection changes in the QR Code site list: /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmboExportSiteQR_SelectedIndexChanged(object sender, EventArgs e) { // Asbestos underpants: try { // Get the current selection of the combo box and make sure it // isn't empty: String site = (string)cmboExportSiteQR.Text; if (!String.IsNullOrEmpty(site)) { // Ask the main form to give us the site parameters for this site. // Since the main form has all the code to do this, we'll ask it // to do the dirty work. SiteParameters siteParams = caller.GetSiteParamsForQRCode(site); // Now we'll generate the text we'll embed into the QR Code. We want // this to be as compact as we can get it, so our "headings" will be // single letters. We'll start off with an identifying header so the // QR Code reader will know the format of our string. We delimite the // string with pipes, which aren't allowed in any of our fields. We // want this to match as closely to the values of the XML export file // for consistency. That means the hash engine and the character // limit fields will need some tweaking. StringBuilder sb = new StringBuilder(); sb.Append("CRYPTNOSv1|" + "S:" + siteParams.Site + "|" + "H:" + HashEngine.HashEnumStringToDisplayHash(siteParams.Hash) + "|" + "I:" + siteParams.Iterations.ToString() + "|" + "C:" + siteParams.CharTypes.ToString() + "|L:"); if (siteParams.CharLimit < 0) { sb.Append("0"); } else { sb.Append(siteParams.CharLimit.ToString()); } // Now that we've built our string, use the QRCodeWriter from ZXing to // build the QR Code image and assign the bitmap to the picture box: byteMatrix = qrCodeWriter.encode(sb.ToString(), BarcodeFormat.QR_CODE, 200, 200); pictureBox1.Image = byteMatrix.ToBitmap(); } // If the selection in the combo box wasn't useful, empty the picture box: else { pictureBox1.Image = null; } } // Similarly, if anything blew up, empty the picture box: catch { pictureBox1.Image = null; } }
/// <summary> /// /// </summary> /// <param name="matrix"></param> /// <returns></returns> public static Bitmap toBitmap(ByteMatrix matrix) { return(matrix.ToBitmap()); //int width = matrix.Width; //int height = matrix.Height; //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 ? Color.Black : Color.White); // } //} //return bmap; }
static public Bitmap textToQRImage(string text, QR_CORRECT_LEV correctLev) { try { if (writer == null) { writer = new QRCodeWriter(); } Hashtable hints = new Hashtable(); switch (correctLev) { case QR_CORRECT_LEV.L: hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L; break; case QR_CORRECT_LEV.M: hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.M; break; case QR_CORRECT_LEV.Q: hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.Q; break; case QR_CORRECT_LEV.H: hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.H; break; default: hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L; break; } ByteMatrix byteMatrix = writer.encode( text, BarcodeFormat.QR_CODE, size, size, hints); return(byteMatrix.ToBitmap()); } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }
//普通二维码 private void btnEncode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); if (String.IsNullOrEmpty(content)) { MessageBox.Show("请输入原文!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); ByteMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 300, 300); Bitmap img = matrix.ToBitmap(); //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } Graphics MyGraphic = Graphics.FromImage(bmpimg); float x = pictureBox1.Width / 2 - 50; float y = pictureBox1.Height - 30; MyGraphic.DrawString(content, this.Font, new SolidBrush(Color.Black), x, y); pictureBox1.Image = bmpimg; //自动保存图片到默认目录 //string filename = Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; //bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); //labShow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static Bitmap Create(string content, Image centralImage) { //构造二维码写码器 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); Hashtable hint = new Hashtable(); hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); hint.Add(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H); //生成二维码 ByteMatrix bm = mutiWriter.encode(content, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300, hint); Bitmap img = bm.ToBitmap(); //要插入到二维码中的图片 Image middlImg = centralImage; //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸) // System.Drawing.Size realSize = mutiWriter.GetEncodeSize(content, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); var realSize = mutiWriter.encode(content, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); //计算插入图片的大小和位置 int middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width); int middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height); int middleImgL = (img.Width - middleImgW) / 2; int middleImgT = (img.Height - middleImgH) / 2; //将img转换成bmp格式,否则后面无法创建 Graphics对象 Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmpimg)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(img, 0, 0); } //在二维码中插入图片 System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg); //白底 MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH); MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH); return(bmpimg); }
private void button1_Click(object sender, EventArgs e) { try { MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); //ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);//二维码 ByteMatrix bm = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.EAN_8, 300, 300);//8位数字 Bitmap img = bm.ToBitmap(); pictureBox1.Width = img.Width; pictureBox1.Height = img.Height; pictureBox1.Image = img; //自动保存图片到当前目录 string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg"; img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg); lbshow.Text = "图片已保存到:" + filename; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public static Bitmap Create(string content, Image centralImage) { MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Hashtable hashtables = new Hashtable() { { EncodeHintType.CHARACTER_SET, "UTF-8" }, { EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H } }; ByteMatrix byteMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300, hashtables); Bitmap bitmap = byteMatrix.ToBitmap(); Image image = centralImage; Size encodeSize = multiFormatWriter.GetEncodeSize(content, BarcodeFormat.QR_CODE, 300, 300); int num = Math.Min((int)(encodeSize.Width / 3.5), image.Width); int num1 = Math.Min((int)(encodeSize.Height / 3.5), image.Height); int width = (bitmap.Width - num) / 2; int height = (bitmap.Height - num1) / 2; Bitmap bitmap1 = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); Graphics graphic = Graphics.FromImage(bitmap1); try { graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.CompositingQuality = CompositingQuality.HighQuality; graphic.DrawImage(bitmap, 0, 0); } finally { if (graphic != null) { graphic.Dispose(); } } Graphics graphic1 = Graphics.FromImage(bitmap1); graphic1.FillRectangle(Brushes.White, width, height, num, num1); graphic1.DrawImage(image, width, height, num, num1); return(bitmap1); }
private void btnBarCode_Click(object sender, EventArgs e) { labShow.Text = ""; string content = txtMsg.Text.Trim(); Regex regex = new Regex("^[0-9]{13}$"); if (!regex.IsMatch(content)) { MessageBox.Show("请输入13位的数字!"); return; } try { MultiFormatWriter writer = new MultiFormatWriter(); ByteMatrix matrix = writer.encode(content, BarcodeFormat.EAN_13, 360, 150); Bitmap bitmap = matrix.ToBitmap(); pictureBox1.Image = bitmap; //自动保存图片到当前目录 SaveFileDialog save = new SaveFileDialog(); save.Title = "保存文件"; save.Filter = "图片文件|*.jpg;*.png;*.bmp|所有文件|*.*"; save.RestoreDirectory = true; save.InitialDirectory = Application.StartupPath; if (save.ShowDialog() == DialogResult.OK) { string filename = save.FileName; bitmap.Save(filename); labShow.Text = "图片已保存到:" + filename; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void txtBarcodeInput_TextChanged(object sender, TextChangedEventArgs e) { if (this.txtBarcodeInput.Text.Length == 13 && barcodeControl != null) { MultiFormatWriter barcodeWriter = new MultiFormatWriter(); Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(delegate { _Global_C_ShowWaitingScreen(); ByteMatrix barcodeData = barcodeWriter.encode(txtBarcodeInput.Text, BarcodeFormat.EAN_13, (int)(barcodeControl.imgBarcodeImg.Width), (int)(barcodeControl.imgBarcodeImg.Height * 0.8)); barcodeControl.imgBarcodeImg.Source = getImageSource(barcodeData.ToBitmap()); })); _Barcode_D_Dispose(txtBarcodeInput.Text); } else if (this.txtBarcodeInput.Text.Length == 16 && membershipControl != null) { MultiFormatWriter barcodeWriter = new MultiFormatWriter(); Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(delegate { _Global_C_ShowWaitingScreen(); })); _Membership_D_Dispose(txtBarcodeInput.Text); } }
protected void Page_Load(object sender, EventArgs e) { //登录用户名 string memid = Request.QueryString["memid"] ?? ""; //微信方提供的APPID,从config配置文件中获取 appid = ConfigurationManager.AppSettings["appid"].ToString(); //接口文档里的参数(目前不知用处) string auth_code = Request.QueryString["auth_code"] ?? ""; //商品描述 string body = Request.QueryString["body"] ?? ""; //微信支付分配的终端设备号 string device_info = Request.QueryString["device_info"] ?? ""; //微信方提供的mch_id,从config配置文件中获取 string mch_id = ConfigurationManager.AppSettings["mch_id"].ToString(); //随机字符串 string nonce_str = TenpayUtil.getNoncestr(); //通知地址,从config配置文件中获取 string notify_url = ConfigurationManager.AppSettings["domain_url"].ToString() + "/wxpay/WeiXinNotifyUrl.aspx"; //订单号 out_trade_no = Request.QueryString["out_trade_no"] ?? ""; //IP string spbill_create_ip = Request.UserHostAddress; //金额 total_fee = Request.QueryString["total_fee"] ?? ""; total_fee = Convert.ToInt32((Convert.ToDecimal(total_fee) * 100)).ToString();//微信的单位为分,所以乘100 //附加数据原样返回 string attach = string.Format("memid={0}|orderid={1}", memid, out_trade_no); //由于二维码支付每次发起支付的订单ID都要唯一,所以这里使用时间字段接在订单末尾获取时截取掉 out_trade_no = out_trade_no + "|" + DateTime.Now.ToString("yyyyMMddHHmmss"); //获取package包 //商户密钥,从config配置文件中获取 string key = ConfigurationManager.AppSettings["key"].ToString(); //创建支付应答对象 RequestHandler packageReqHandler = new RequestHandler(Context); //初始化 packageReqHandler.init(); packageReqHandler.setParameter("appid", appid); packageReqHandler.setParameter("body", body); packageReqHandler.setParameter("mch_id", mch_id); packageReqHandler.setParameter("nonce_str", nonce_str.ToLower()); packageReqHandler.setParameter("notify_url", notify_url); packageReqHandler.setParameter("out_trade_no", out_trade_no); packageReqHandler.setParameter("spbill_create_ip", spbill_create_ip); packageReqHandler.setParameter("total_fee", total_fee);//商品金额,以分为单位(money * 100).ToString() packageReqHandler.setParameter("trade_type", "NATIVE"); packageReqHandler.setParameter("product_id", out_trade_no); string sign = packageReqHandler.createMd5Sign("key", key); packageReqHandler.setParameter("sign", sign); string data = packageReqHandler.parseXML(); var result = TenPayV3.Unifiedorder(data, "https://api.mch.weixin.qq.com/pay/unifiedorder"); var res = XDocument.Parse(result); string code_url = string.Empty; try { if (res.Element("xml").Element("return_code").Value == "SUCCESS") { code_url = res.Element("xml").Element("code_url").Value; } } catch (Exception) { } if (!string.IsNullOrEmpty(code_url)) { //生成二维码图片 MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter(); ByteMatrix bm = mutiWriter.encode(code_url, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300); Bitmap img = bm.ToBitmap(); MemoryStream ms = new MemoryStream(); img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); HttpContext.Current.Response.ClearContent(); //需要输出图象信息 要修改HTTP头 HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.BinaryWrite(ms.ToArray()); img.Dispose(); ms.Dispose(); HttpContext.Current.Response.End(); } }