示例#1
0
        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);
        }
示例#2
0
        public static string CreateQRCodePic(string info, string midImgPath, string saveDir, string fileName, ErrorCorrectionLevel ecLevel, int width = 300, int height = 300)
        {
            string QRImgPath = string.Empty;

            if (string.IsNullOrEmpty(info))//要存储的信息不能为空,否则异常
            {
                return(QRImgPath);
            }
            try
            {
                MultiFormatWriter writer = new MultiFormatWriter();
                //构造创建参数;
                Hashtable hints = new Hashtable();
                hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                hints.Add(EncodeHintType.ERROR_CORRECTION, ecLevel);
                Bitmap QRImg  = writer.encode(info, BarcodeFormat.QR_CODE, width, height, hints).ToBitmap();                           //生成不带图片的BitMap;
                Image  midImg = null;
                if (!string.IsNullOrEmpty(midImgPath) && File.Exists(midImgPath) && legalExts.Contains(Path.GetExtension(midImgPath))) //验证图片存在且后缀名符合
                {
                    try                                                                                                                //确保即使中间图片加载失败也能生成无中间图的QR码
                    {
                        midImg = Image.FromFile(midImgPath);
                    }
                    catch { }
                }

                Bitmap bmpimg = new Bitmap(QRImg.Width, QRImg.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(QRImg, 0, 0);
                }
                if (midImg != null)
                {
                    Size QRSize = writer.GetEncodeSize(info, BarcodeFormat.QR_CODE, width, height);
                    //计算中间图片大小和位置
                    int      midImgW   = Math.Min((int)(QRSize.Width * midImgScale), midImg.Width);
                    int      midImgH   = Math.Min((int)(QRSize.Height * midImgScale), midImg.Height);
                    int      midImgL   = (QRImg.Width - midImgW) / 2;
                    int      midImgT   = (QRImg.Height - midImgH) / 2;
                    Graphics myGraphic = System.Drawing.Graphics.FromImage(bmpimg);
                    myGraphic.FillRectangle(Brushes.White, midImgL, midImgT, midImgW, midImgH);
                    myGraphic.DrawImage(midImg, midImgL, midImgT, midImgW, midImgH);
                    myGraphic.Dispose();
                    midImg.Dispose();
                }
                if (!Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }
                QRImgPath = Path.Combine(saveDir, fileName + ".jpg");
                bmpimg.Save(QRImgPath, ImageFormat.Jpeg);
            }
            catch (Exception e) {
            }
            return(QRImgPath);
        }
示例#3
0
        //带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);
            }
        }
示例#4
0
        /// <summary>
        /// 构建 二维码图片
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        /// <param name="midImg">中心图</param>
        /// <returns></returns>
        public static Bitmap Encode(string content, int width = 430, int height = 430, Image midImg = null)
        {
            //构造二维码写码器
            var mutiWriter = new MultiFormatWriter();
            var hint       = new Hashtable
            {
                { EncodeHintType.CHARACTER_SET, "UTF-8" },
                { EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H }
            };
            //生成二维码
            var bm  = mutiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hint);
            var img = bm.ToBitmap();

            if (midImg == null)
            {
                return(img);
            }

            var middlImg = midImg;
            //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
            var realSize = mutiWriter.GetEncodeSize(content, BarcodeFormat.QR_CODE, width, height);
            //计算插入图片的大小和位置
            var middleImgW = Math.Min((int)(realSize.Width / 3.5), middlImg.Width);
            var middleImgH = Math.Min((int)(realSize.Height / 3.5), middlImg.Height);
            var middleImgL = (img.Width - middleImgW) / 2;
            var middleImgT = (img.Height - middleImgH) / 2;

            //将img转换成bmp格式,否则后面无法创建 Graphics对象
            var bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (var 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);
            };

            //在二维码中插入图片
            var MyGraphic = Graphics.FromImage(bmpimg);

            //白底
            MyGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);
            MyGraphic.DrawImage(middlImg, middleImgL, middleImgT, middleImgW, middleImgH);

            return(bmpimg);
        }
示例#5
0
        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);
        }