生成二维码与返回流

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;

namespace Glonee.Handler
{
    /// <summary>
    /// ErWeiMa 的摘要说明
    /// </summary>
    public class ErWeiMa : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string strSource = context.Request.Params["strSource"];
            string text = "";
            int width = 250;
            int height = 250;
            string fontName = "楷体";
            Image image = (GetTwoDimensionCode(strSource,text,width,height,fontName));
            context.Response.ContentType = "image/jpeg";
            image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public static Bitmap GetTwoDimensionCode(string strSource,
           string text, int width, int height, string fontName)
        {
            // 创建Bitmap对象
            Bitmap bmp = new Bitmap(width, height);
            // 从image创建 Graphics对象
            Graphics objGraphics = Graphics.FromImage(bmp);
            // 填上背景色
            objGraphics.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
            // 
            ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder =
                new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
            // 设置编码方法
            qrCodeEncoder.QRCodeEncodeMode =
                ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
            // 设置大小
            qrCodeEncoder.QRCodeScale = 3;
            // 适用于信息量较少的情形,图像越小保存的信息量越少
            // qrCodeEncoder.QRCodeScale = 4;
            // 设置版本
            qrCodeEncoder.QRCodeVersion = 0;
            // 设置错误校验的级别,正因为二维码有纠错能力,才能够加入logo
            qrCodeEncoder.QRCodeErrorCorrect =
                ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;
            Image image = qrCodeEncoder.Encode(strSource, Encoding.GetEncoding("utf-8"));
            // 写入二维码
            int x = (int)(width - image.Width) / 2;
            int y = (int)(height - image.Height) / 2;
            objGraphics.DrawImage(image, new Point(x, y));
// 添加Logo图标
            image = Image.FromFile(@"G:\ASP.net\S2结业项目\MI\MI\img\logo-footer.png");
            x = (int)(width - image.Width) / 2;
            y = (int)(height - image.Height) / 2;
            objGraphics.DrawImage(image, new Point(x, y));
            // 写入字符串
            //objGraphics.DrawString(text, new Font(fontName, 13, FontStyle.Bold), 
            //    Brushes.Black, new PointF(43, 15));
            return bmp;
        }
    }
}


想要使用这个返回的值 就直接src 去访问这个handler

  

posted @ 2018-08-06 22:45  ZaraNet  阅读(566)  评论(0编辑  收藏  举报