Ejemplo n.º 1
0
        //生成条形码
        private void button1_Click(object sender, EventArgs e)
        {
            lbshow.Text = "";
            Regex rg = new Regex("^[0-9]{13}$");

            if (!rg.IsMatch(txtMsg.Text))
            {
                MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字");
                return;
            }

            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                ByteMatrix        bm         = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.EAN_13, 363, 150);
                Bitmap            img        = bm.ToBitmap();
                pictureBox1.Image = img;

                //自动保存图片到当前目录
                string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg";
                img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                lbshow.Text = "图片已保存到:" + filename;
            }
            catch (Exception ee)
            { MessageBox.Show(ee.Message); }
        }
Ejemplo n.º 2
0
        //Look at http://code.google.com/p/zxing/wiki/BarcodeContents for formatting

        /// <summary>
        /// Returns a QRCode with the data embedded
        /// </summary>
        /// <param name="data">The data to embed in a QRCode.</param>
        public Task<ImageResult> QRCode(string data)
        {
            return AsyncHelper.RunAsync(() =>
            {
                var qrCode = new MultiFormatWriter();
                var byteIMG = qrCode.encode(data, BarcodeFormat.QR_CODE, 200, 200);
                sbyte[][] img = byteIMG.Array;
                var bmp = new Bitmap(200, 200);
                var g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                for (var y = 0; y <= img.Length - 1; y++)
                {
                    for (var x = 0; x <= img[y].Length - 1; x++)
                    {
                        g.FillRectangle(
                            img[y][x] == 0 ? Brushes.Black : Brushes.White, x, y, 1, 1);
                    }
                }

                var stream = new System.IO.MemoryStream();
                bmp.Save(stream, ImageFormat.Jpeg);
                var imageBytes = stream.ToArray();
                stream.Close();

                return this.Image(imageBytes, "image/jpeg");
            });
        }
Ejemplo n.º 3
0
        private void generate_barcode(String queryString, int width, int height, PictureBox targetPictureBox)
        {
            Regex rg = new Regex("^[0-9]{11}$");

            if (!rg.IsMatch(queryString))
            {
                // MessageBox.Show("本例子采用EAN_13编码,需要输入13位数字");
                MessageBox.Show("本例子采用EAN_11编码,需要输入11位数字");
                return;
            }

            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                ByteMatrix        bm         = mutiWriter.encode(queryString, com.google.zxing.BarcodeFormat.EAN_13, 363, 150);
                Bitmap            img        = bm.ToBitmap();
                targetPictureBox.Image = img;

                //自动保存图片到当前目录
                //string filename = System.Environment.CurrentDirectory + "\\EAN_13" + DateTime.Now.Ticks.ToString() + ".jpg";
                //img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ee)
            { MessageBox.Show(ee.Message); }
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        private void generate_qrcode(String queryString, Image logoImage, int size, PictureBox targetPictureBox)
        {
            try
            {
                //构造二维码写码器
                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(queryString, com.google.zxing.BarcodeFormat.QR_CODE, size, size, hint);
                Bitmap     img = bm.ToBitmap();

                //要插入到二维码中的图片
                Image middlImg = logoImage;
                //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
                System.Drawing.Size realSize = mutiWriter.GetEncodeSize(TrackingNumber.Text, 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);

                targetPictureBox.Image = bmpimg;

                //自动保存图片到当前目录
                //string filename = System.Environment.CurrentDirectory + "\\QR" + DateTime.Now.Ticks.ToString() + ".jpg";
                //bmpimg.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                //lbshow.Text = "图片已保存到:" + filename;
            }
            catch (Exception ee)
            { MessageBox.Show(ee.Message); }
        }
Ejemplo n.º 7
0
        //生成二维码
        private void button2_Click(object sender, EventArgs e)
        {
            lbshow.Text = "";
            try
            {
                MultiFormatWriter mutiWriter = new com.google.zxing.MultiFormatWriter();
                ByteMatrix        bm         = mutiWriter.encode(txtMsg.Text, com.google.zxing.BarcodeFormat.QR_CODE, 300, 300);
                Bitmap            img        = bm.ToBitmap();
                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 ee)
            { MessageBox.Show(ee.Message); }
        }
Ejemplo n.º 8
0
        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();
            }
        }
 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);
     }
 }