在图片上添加二维码的写法

 public MemoryStream ProcessRequest()
        {
            String data = @"二维码内容";
            if (!string.IsNullOrEmpty(data))
            {
                //二维码生成
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                //设置编码模式  
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                //设置编码测量度,值越大生成的二维码图片像素越高,尺寸越大(建议设置为4) 
                qrCodeEncoder.QRCodeScale = 4;
                //设置编码版本  
                qrCodeEncoder.QRCodeVersion = 0;
                //设置编码错误纠正  
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                //生成二维码图片  
                System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
                MemoryStream MStream = new MemoryStream();
                image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);//保存在MStream1中
                MemoryStream MStream1 = new MemoryStream();

                //根据URL获得要打码的目标图片
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"要打码的图片地址(URL)");
                WebResponse response = request.GetResponse();//获得响应
                Image img = Image.FromStream(response.GetResponseStream());///实例化,得到img
                CombinImage(img, image).Save(MStream1, System.Drawing.Imaging.ImageFormat.Jpeg);
                image.Dispose();
                return MStream1;
            }
            else 
            {
                return null;
            }
        }

        /// <summary>
        /// 调用此函数后使此两种图片合并,类似相册,有个
        /// 背景图,中间贴自己的目标图片
        /// </summary>
        /// <param name="imgBack">粘贴的源图片</param>
        /// <param name="destImg">粘贴的目标图片</param>
        public static Image CombinImage(Image imgBack, Image destImg)
        {
            Graphics g = Graphics.FromImage(imgBack);
            g.FillRectangle(System.Drawing.Brushes.White, 20,20,10,10);//相片四周刷一层黑色边框
            //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
            g.DrawImage(destImg, 20, 20, 100, 100);

            // Create font and brush.
            Font drawFont = new Font("Arial", 13);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create point for upper-left corner of drawing.
            PointF drawPoint = new PointF(150.0F, 50.0F);
            // Set format of string.
            StringFormat drawFormat = new StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.NoWrap;
            g.DrawString("手机扫码查看", drawFont, drawBrush, 10, 120);
            GC.Collect();
            return imgBack;
        }

 

posted @ 2017-05-05 17:24  洛晨随风  阅读(2328)  评论(0编辑  收藏  举报