.net 将网址生成带有logo图片的二维码图片

        /// 
        /// 利用返回的地址生成二维码  此功能还可以用来做二维码防伪
        /// 
        private string Creat_ewm(string yemian_path)//yemian_path是生成的二维码图片地址 以http://格式开头的网址
        {
            int high = 600, width = 600;//设置图片的高和宽
            Bitmap imag_map = BarcodeHelper.Generate3(yemian_path, high, width);
            string Path = System.Web.HttpContext.Current.Server.MapPath("~/Jzpg/ewmfw/images");//存放生成的二维码图片的文件夹
            string htmlfilename = Path + "/" + Guid.NewGuid().ToString() + ".png";//保存新生成的二维码png图片的存放地址
            imag_map.Save(htmlfilename, System.Drawing.Imaging.ImageFormat.Png);//保存图片
            return htmlfilename;//返回二维码图片的地址
        }

        /// 
        /// 生成带Logo的二维码
        /// 
        /// 内容
        /// 宽度
        /// 高度
        public static Bitmap Generate3(string text, int width, int height)
        {
            //logoPath为Logo 图片的地址
            string logoPath = System.AppDomain.CurrentDomain.BaseDirectory + "Jzpg\\ewmfw\\images\\center.jpg";
            Bitmap logo = new Bitmap(logoPath);
            //构造二维码写码器
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary hint = new Dictionary();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //hint.Add(EncodeHintType.MARGIN, 2);//旧版本不起作用,需要手动去除白边,根据自己电脑情况调试,判断此行代码是否需要使用

            //生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width + 30, height + 30, hint);
            //bm = deleteWhite(bm);
            BarcodeWriter barcodeWriter = new BarcodeWriter();
            Bitmap map = barcodeWriter.Write(bm);

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

            //计算插入图片的大小和位置
            int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
            int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
            int middleL = (map.Width - middleW) / 2;
            int middleT = (map.Height - middleH) / 2;

            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, width, height);
                //白底将二维码插入图片
                g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
                g.DrawImage(logo, middleL, middleT, middleW, middleH);
            }
            return bmpimg;
        }

 

你可能感兴趣的:(.net,C#,.net)