C# MVC 实现生成二维码条形码给予Pos核销

哎,先实现效果吧。


上代码,这段方法不需要动, 添加引用就好,

       #region 二维码部分

        /// <summary>
        /// 128条码调用生成
        /// </summary>
        /// <param name="Ncode">条码编号</param>
        private void Get128BarCode(string Ncode)
        {
            Code128 _Code128 = new Code128();
            _Code128.Height = 90;
            _Code128.Magnify = 1;
            _Code128.ValueFont = new Font("宋体", 14);

            System.Drawing.Image _CodeImage = _Code128.GetCodeImage(Ncode, Code128.Encode.Code128A);

            System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
            _CodeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //_CodeImage.Save(@"C:\1.jpeg");
            //_CodeImage.Save(@"C:\1.BMP");
            //_CodeImage.Save(@"C:\1.GIF");

            Response.ContentType = "image/jpeg";
            Response.Clear();
            Response.BufferOutput = true;
            Response.BinaryWrite(_Stream.GetBuffer());
            Response.Flush();
        }

        private void get_barcode(string code)
        {
            Code39 _Code39 = new Code39();
            _Code39.Height = 90;
            _Code39.Magnify = 1;
            _Code39.ViewFont = new Font("宋体", 14);


            System.Drawing.Image _CodeImage = _Code39.GetCodeImage(code, Code39.Code39Model.Code39Normal, true);

            System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
            _CodeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //_CodeImage.Save(@"C:\1.jpeg");
            //_CodeImage.Save(@"C:\1.BMP");
            //_CodeImage.Save(@"C:\1.GIF");

            Response.ContentType = "image/jpeg";
            Response.Clear();
            Response.BufferOutput = true;
            Response.BinaryWrite(_Stream.GetBuffer());
            Response.Flush();
        }


        /// <summary>  
        /// 生成二维码图片  
        /// </summary>  
        /// <param name="codeNumber">要生成二维码的字符串</param>       
        /// <param name="size">大小尺寸</param>  
        /// <returns>二维码图片</returns>  
        public void Create_ImgCode(string codeNumber, int size)
        {
            //创建二维码生成类  
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //设置编码模式  
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度  
            qrCodeEncoder.QRCodeScale = size;
            //设置编码版本  
            qrCodeEncoder.QRCodeVersion = 0;
            //设置编码错误纠正  
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //生成二维码图片  
            System.Drawing.Bitmap _CodeImage = qrCodeEncoder.Encode(codeNumber);

            System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
            _CodeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //_CodeImage.Save(@"C:\1.jpeg");
            //_CodeImage.Save(@"C:\1.BMP");
            //_CodeImage.Save(@"C:\1.GIF");

            Response.ContentType = "image/jpeg";
            Response.Clear();
            Response.BufferOutput = true;
            Response.BinaryWrite(_Stream.GetBuffer());
            Response.Flush();
            //return image;
        }

        #endregion

用的是MVC 一下为view调用的Action,这个view是要有的,可以不写数据。里面生成什么样就是什么样


        public ActionResult Cimg()
        {
            string Ncode = Request.QueryString["Ncode"];
            string flag = Request.QueryString["flag"];
            if (flag == "2")
            {
                Create_ImgCode(Ncode, 4);
            }
            else if (flag == "1")
            {
                get_barcode(Ncode);
            }
            else if ("128" == flag)
            {
                Get128BarCode(Ncode);
            }
            return View();
        }

下面就是调用视图 Cimg的代码了,




        <div class="ma benefit_coupon_detail_content">
                    <p>
                        <img style="height:104px; width:104px;" src="@Url.Action("Cimg")?flag=2&[email protected]_coupon_no">
                    </p>
                    <img style="width:204px; height:60px" src="@Url.Action("Cimg")?flag=1&[email protected]_coupon_no" class="tiaom">
                </div>

传2为二维码,1为条形码 ,Flag就是你要传的生成的字符串咯。

你可能感兴趣的:(C#,二维码,条形码)