MVC生成二维码

        [HttpPost]
        public ActionResult ErWeiMaIndex(FormCollection formCollection)
        {
            string strContent = formCollection["txtQRContext"].ToString();
            string strPicUrl = formCollection["picUrl"].ToString();
            MemoryStream ms = new MemoryStream();

            #region 生成二维码
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 7;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            Image image = qrCodeEncoder.Encode(strContent, System.Text.Encoding.UTF8); //kedee 增加utf-8编码,可支持中文汉字  
            Bitmap map = new Bitmap(image, new Size(200, 200));
            #endregion

            map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //  将图片内存流转成base64,图片以DataURI形式显示
            string strUrl = "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());
            image.Dispose();
            ms.Dispose();
            //  显示
            ViewBag.src = strUrl;
            return View();
        }

使用ThoughtWorks.QRCode.dll组件

生成中间包含图标的二维码

前台部分:

tip:标签encType="multipart/form-data"和标签name="AAA"必写,缺少的话,后台获取不到上传的文件。

@{
    ViewBag.Title = "ErWeiMaIndex";
}

ErWeiMaIndex

@using (Html.BeginForm("ErWeiMaIndex", "ErWeiMa", FormMethod.Post, new { encType = "multipart/form-data" })) { @Html.TextBox("txtQRContext")Html.Raw("
");
请选择logo图片: }
后台部分:图标位置的结算以后改进

        #region 不包含图标的二维码
        /*
        /// 
        /// 不包含图标的二维码
        /// 
        /// 
        /// 
        [HttpPost]
        public ActionResult ErWeiMaIndex(FormCollection formCollection)
        {
            string strContent = formCollection["txtQRContext"].ToString();
            string strPicUrl = formCollection["picUrl"].ToString();
            MemoryStream ms = new MemoryStream();

            #region 生成二维码
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 7;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            Image image = qrCodeEncoder.Encode(strContent, System.Text.Encoding.UTF8); //kedee 增加utf-8编码,可支持中文汉字  
            Bitmap map = new Bitmap(image, new Size(200, 200));
            #endregion

            map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //  将图片内存流转成base64,图片以DataURI形式显示
            string strUrl = "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());
            image.Dispose();
            ms.Dispose();
            //  显示
            ViewBag.src = strUrl;
            return View();
        }*/
        #endregion

        #region 包含图标的二维码
        [HttpPost]
        public ActionResult ErWeiMaIndex(FormCollection formCollection)
        {
            string strContextText = formCollection["txtQRContext"].ToString();
            HttpPostedFileBase uploadPic = Request.Files[0];

            #region 生成二维码图片
            Bitmap btQr = new Bitmap(200, 200);
            QRCodeEncoder qrCodeEncode = new QRCodeEncoder();
            qrCodeEncode.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncode.QRCodeScale = 4;
            qrCodeEncode.QRCodeVersion = 7;
            btQr = qrCodeEncode.Encode(strContextText,Encoding.UTF8);
            #endregion

            #region 生成新图
            Bitmap btNewImage = GetNewImage(btQr, uploadPic);
            #endregion
            //  声明一个流
            MemoryStream ms = new MemoryStream();
            //  将图片保存到流当中去
            btNewImage.Save(ms, ImageFormat.Gif);
            //  将图片设置成dataURI格式,赋值给src
            ViewBag.src = "data:image/gif;base64," + Convert.ToBase64String(ms.ToArray());

            return View();
        }
        /// 
        /// 生成包含logo图标的二维码
        /// 
        /// 无logo的二维码
        /// logo
        /// 
        private Bitmap GetNewImage(Bitmap btQr, HttpPostedFileBase uploadPic)
        {
            int intBtLogoWidth = btQr.Width / 3;
            int intBtLogoHeigth = btQr.Height / 3;

            #region 获取并处理logo
            Image imgLogo = Image.FromStream(uploadPic.InputStream);
            Bitmap btLogo = new Bitmap(imgLogo, intBtLogoWidth, intBtLogoHeigth);
            #endregion

            #region 合并图片,使用Graphice(画布)
            Graphics g = Graphics.FromImage(btQr);
            g.DrawImage(Image.FromStream(uploadPic.InputStream), intBtLogoWidth, intBtLogoHeigth, intBtLogoWidth, intBtLogoHeigth);
            g.Dispose();
            #endregion

            return btQr;
        }
        #endregion




你可能感兴趣的:(MVC)