• 二维码图片以字符串的形式保存DB,已文件流显示页面上


    以下是生成二维码的方法,我只用其中一个方法 这个需要引用ZXing.DLL 

    链接:https://pan.baidu.com/s/1mCTwHiAm_awtsPcibAotZw
    提取码:ufp6 

     public class QRcode
        {
            //需要引用ZXing.DLL  可以百度一下  一个很好用的二维码生成程序集《DLL文件啦》
            //使用案例:Bitmap img3 = GenByZXingNet("");
            //使用案例:img3.Save(Server.MapPath(@"	estImgerweima.png"));
            /// <summary>
            /// 生成二维码
            /// </summary>
            /// <param name="msg">二维码信息</param>
            /// <param name="codeSizeInPixels">正方形 边长</param>
            /// <returns>图片</returns>
            public static Bitmap GenByZXingNet(string msg, int codeSizeInPixels = 250)
            {
                BarcodeWriter writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
                writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
    
                writer.Options.Height = writer.Options.Width = codeSizeInPixels;
                writer.Options.Margin = 0;//设置边框1            ZXing.Common.BitMatrix bm = writer.Encode(msg);
                Bitmap img = writer.Write(msg);
                return img;
            }
    
            #endregion
    
            #region C#生成带有Log的二维码
            /// <summary>
            /// 生成带Logo的二维码
            /// </summary>
            /// <param name="text">文本内容</param>
            static void Generate(string text, string LogoPth, string serverPth, ImageFormat imgFrt)
            {
                //Logo 图片
                Bitmap logo = new Bitmap(LogoPth);
                //构造二维码写码器
                MultiFormatWriter writer = new MultiFormatWriter();
                Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
                hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
                hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
    
                //生成二维码 
                BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300, hint);
                BarcodeWriter barcodeWriter = new BarcodeWriter();
                Bitmap map = barcodeWriter.Write(bm);
    
    
                //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
                int[] rectangle = bm.getEnclosingRectangle();
    
                //计算插入图片的大小和位置
                int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
                int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
                int middleL = (map.Width - middleW) / 2;
                int middleT = (map.Height - middleH) / 2;
    
                //将img转换成bmp格式,否则后面无法创建Graphics对象
                Bitmap bmpimg = new Bitmap(map.Width, map.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(map, 0, 0);
                }
                //将二维码插入图片
                Graphics myGraphic = Graphics.FromImage(bmpimg);
                //白底
                myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
                myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
    
                //保存成图片
                bmpimg.Save(serverPth, imgFrt);
            }
            #endregion
    
            #region ZXing生成条形码
            /// <summary>
            /// 文本内容
            /// </summary>
            /// <param name="text">只支持数字 只支持偶数个 最大长度80</param>
            /// <param name="ServerPTh">存储路径 @"H:桌面截图generate2.png"</param>
            public static void Generate(string text, string ServerPth, ImageFormat imgFrt)
            {
                BarcodeWriter writer = new BarcodeWriter();
                //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
                //如果想生成可识别的可以使用 CODE_128 格式
                //writer.Format = BarcodeFormat.ITF;
                writer.Format = BarcodeFormat.CODE_128;
                EncodingOptions options = new EncodingOptions()
                {
                    Width = 150,
                    Height = 50,
                    Margin = 2
                };
                writer.Options = options;
                Bitmap map = writer.Write(text);
                string filename = ServerPth;//请注意 保存格式和文件扩展名一致性
                map.Save(filename, imgFrt);
            }
    
    
            /// <summary>
            /// 文本内容  生成条形码
            /// </summary>
            /// <param name="text">只支持数字 最大长度80</param>
            /// <param name="ServerPTh">文件夹名称</param>
            /// <param name="fileName">文件名</param>
            public static string GenerateTxm(string text, string category, string fileName, ImageFormat imgFrt)
            {
                var uploadPath = System.Configuration.ConfigurationSettings.AppSettings["UploadPath"];
    
                //文件保存目录路径 
                string dirTempPath = uploadPath + "//" + category;
                if (!Directory.Exists(dirTempPath))
                {
                    Directory.CreateDirectory(dirTempPath);
                }
                string ServerPth = dirTempPath + "//" + fileName;//文件绝对路径
                BarcodeWriter writer = new BarcodeWriter();
                //使用ITF 格式,不能被现在常用的支付宝、微信扫出来
                //如果想生成可识别的可以使用 CODE_128 格式
                //writer.Format = BarcodeFormat.ITF;
                writer.Format = BarcodeFormat.CODE_128;
                EncodingOptions options = new EncodingOptions()
                {
                    Width = 150,
                    Height = 50,
                    Margin = 2
                };
                writer.Options = options;
                Bitmap map = writer.Write(text);
                string filename = ServerPth;//请注意 保存格式和文件扩展名一致性
                map.Save(filename, imgFrt);
                return ServerPth;
            }
            #endregion
    
            #region 二维码 条形码读取
            /// <summary>
            /// 二维码 条形码读取
            /// </summary>
            /// <param name="filename">文件路径</param>
            /// <returns></returns>
            public static string Read(string filename)
            {
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                Bitmap map = new Bitmap(filename);
                Result result = reader.Decode(map);
                return result == null ? "" : result.Text;
            }
            #endregion
    
            #region C#生成特定颜色二维码-基础版本
            //需要引用ZXing.DLL  可以百度一下  一个很好用的二维码生成程序集《DLL文件啦》
            //使用案例:Bitmap img3 = GenByZXingNet("");
            //使用案例:img3.Save(Server.MapPath(@"	estImgerweima.png"));
            /// <summary>
            /// 生成二维码
            /// </summary>
            /// <param name="msg">二维码信息</param>
            /// <param name="codeSizeInPixels">正方形 边长</param>
            /// <returns>图片</returns>
            public static Bitmap GenByZXingNet_Color(string msg, int codeSizeInPixels = 250)
            {
                BarcodeWriter writer = new BarcodeWriter();
                writer.Renderer = new ZXing.Rendering.BitmapRenderer { Background = Color.Red, Foreground = Color.Blue };
                writer.Format = BarcodeFormat.QR_CODE;
                writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
                writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
    
                writer.Options.Height = writer.Options.Width = codeSizeInPixels;
                writer.Options.Margin = 0;//设置边框1            ZXing.Common.BitMatrix bm = writer.Encode(msg);
                Bitmap img = writer.Write(msg);
                return img;
            }
    
        }
    View Code
                Bitmap b = QRcode.GenByZXingNet("二维码内容字符串");
                MemoryStream ms = new MemoryStream();
                b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] bytes = ms.GetBuffer();
                string textString = System.Convert.ToBase64String(bytes);//存DBd的字符串
                ms.Close();
    

      

    string ImgString;//存DB的字符串
    byte[] arrBuffer = System.Convert.FromBase64String(ImgString);//转换成文件流
    

      页面上展示

                        <img src="data:image/jpeg;base64,{{Data.ImgString}}">
    

      我上面是后端的写法,现在用下面的写法比较简单

    使用jquery.qrcode生成二维码

     https://www.cnblogs.com/zhengweijie/p/6911834.html 
  • 相关阅读:
    CSS:Tutorial three
    CSS:Tutorial two
    CSS:Tutorial one
    JavaScript: 代码简洁之道
    python数据结构:pandas(1)
    python数据结构:numpy
    python函数之format()
    算法(1)
    python基础面试题:(1)
    python学习笔记(13):python并发编程以及系统常用模块
  • 原文地址:https://www.cnblogs.com/youngharvard/p/10484795.html
Copyright © 2020-2023  润新知