/// <summary>
        /// 创建带图片的二维码
        /// </summary>
        /// <param name="message">信息</param>
        /// <param name="middleImg">图片</param>
        /// <param name="pixel">大小</param>
        /// <returns></returns>
        private Bitmap CreateQrCode(string message, string imagePath, int pixel)
        {
            using (Image middleImg = new Bitmap(imagePath))
            {
                //构造二维码写码器
                MultiFormatWriter mutiWriter = new MultiFormatWriter();
                Dictionary <EncodeHintType, object> encodeHint = new Dictionary <EncodeHintType, object>();
                encodeHint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                encodeHint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

                //生成二维码
                BitMatrix bitMatrix = mutiWriter.encode(message, BarcodeFormat.QR_CODE, pixel, pixel, encodeHint);
                ZKWebNet.BarcodeWriter barcodeWriter = new ZKWebNet.BarcodeWriter();

                Bitmap bitmap = barcodeWriter.Write(bitMatrix);

                //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
                int[] rectangle = bitMatrix.getEnclosingRectangle();

                //计算插入图片的大小和位置
                int middleImgW = Math.Min((int)(rectangle[2] / 3.5), middleImg.Width);
                int middleImgH = Math.Min((int)(rectangle[3] / 3.5), middleImg.Height);
                int middleImgL = (bitmap.Width - middleImgW) / 2;
                int middleImgT = (bitmap.Height - middleImgH) / 2;

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

                using (Graphics graphics = Graphics.FromImage(bmpimg))
                {
                    graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    graphics.SmoothingMode      = SmoothingMode.HighQuality;
                    graphics.CompositingQuality = CompositingQuality.HighQuality;
                    graphics.DrawImage(bitmap, 0, 0);
                }

                //在二维码中插入图片
                using (Graphics myGraphic = Graphics.FromImage(bmpimg))
                {
                    //白底
                    myGraphic.FillRectangle(Brushes.White, middleImgL, middleImgT, middleImgW, middleImgH);

                    myGraphic.DrawImage(middleImg, middleImgL, middleImgT, middleImgW, middleImgH);
                }

                bitmap.Dispose();

                return(bmpimg);
            }
        }
Example #2
0
        /// <summary>
        /// 创建一维码
        /// </summary>
        /// <param name="message"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private Bitmap CreateBarCode(string message, int width, int height)
        {
            CodaBarWriter codaBarWriter = new CodaBarWriter();
            BitMatrix     bitMatrix     = codaBarWriter.encode(message, BarcodeFormat.ITF, width, height);

            ZKWebNet.BarcodeWriter barcodeWriter = new ZKWebNet.BarcodeWriter();
            barcodeWriter.Options = new EncodingOptions()
            {
                Margin      = 3,
                PureBarcode = true
            };
            Bitmap bitmap = barcodeWriter.Write(bitMatrix);

            return(bitmap);
        }
        /// <summary>
        /// 创建不带图片的二维码
        /// </summary>
        /// <param name="message">信息</param>
        /// <param name="pixel">大小</param>
        /// <returns></returns>
        private Bitmap CreateQrCode(string message, int pixel)
        {
            ZKWebNet.BarcodeWriter barcodeWriter   = new ZKWebNet.BarcodeWriter();
            EncodingOptions        encodingOptions = new QrCodeEncodingOptions
            {
                DisableECI      = true,
                CharacterSet    = "UTF-8",
                Width           = pixel,
                Height          = pixel,
                ErrorCorrection = ErrorCorrectionLevel.H,
            };

            barcodeWriter.Format  = BarcodeFormat.QR_CODE;
            barcodeWriter.Options = encodingOptions;

            Bitmap bitmap = barcodeWriter.Write(message);

            return(bitmap);
        }