二维码生成基于Gma.QrCodeNet.Encoding

参考:http://www.cnblogs.com/vmyspace/archive/2012/04/05/2433233.html

今天,项目需要,参考资料写了个二维码生成后台  基于Gma.QrCodeNet.Encoding

前台html直接请求地址,固定图片高宽就能用。

 

using System;
using System.Collections.Generic;
using System.Web;
using Gma.QrCodeNet.Encoding;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

 


namespace TestWeb.erweima_test
{
    /// <summary>
    /// Handler2 的摘要说明
    /// </summary>
    public class Handler2 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (!string.IsNullOrEmpty(context.Request["qrdata"].ToString()))
            {
                CreateQr(context);
            }
           
        }

        public void CreateQr(HttpContext context)
        {
            QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
            QrCode qrCode = new QrCode();
            //String data = "0150204whxtl/?type=invate";
            String data = context.Request["qrdata"].ToString() ;
            //String data = context.Request.QueryString["qrdata"].ToString();
            System.Drawing.Bitmap image;
            qrCode = qrEncoder.Encode(data);
            image = new Bitmap(qrCode.Matrix.Width * 5, qrCode.Matrix.Height * 5);
            image.SetResolution(180f, 180f);
            Graphics gdiobj = Graphics.FromImage(image);
            gdiobj.CompositingQuality = CompositingQuality.HighQuality;
            gdiobj.SmoothingMode = SmoothingMode.HighQuality;
            gdiobj.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;

            for (Int32 i = 0; i < qrCode.Matrix.Height; i++)
            {
                for (Int32 j = 0; j < qrCode.Matrix.Width; j++)
                {
                    if (qrCode.Matrix.InternalArray[i, j])
                    {
                        gdiobj.FillRectangle(new SolidBrush(Color.Black), i * 5, j * 5, 5, 5);
                    }
                }
            }

            System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
            ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 800L);

            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);
            // image.Save(MStream, null, ep);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Png";
            context.Response.BinaryWrite(MStream.ToArray());
        
        
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。