码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-02-10 14:46:02      阅读:5668      评论:0      收藏:0      [点我收藏+]

标签:

参考: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;
            }
        }
    }
}

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

标签:

原文地址:http://www.cnblogs.com/wangxianchao/p/4283568.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!