[セットトップ][Asp.Net]QRCode生成QRコード(続き)-QRコードピクチャの中心にLogoまたは画像を追加

3899 ワード

<%@ WebHandler Language="C#" Class="GetQRCode" %>

using System;
using System.Web;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

public class GetQRCode : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        String data = context.Request["CodeText"];
        if (!string.IsNullOrEmpty(data))
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            System.Drawing.Image image = qrCodeEncoder.Encode(data);
            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);

            System.IO.MemoryStream MStream1 = new System.IO.MemoryStream();
            CombinImage(image, context.Server.MapPath("~/images/201292891051540.jpg")).Save(MStream1, System.Drawing.Imaging.ImageFormat.Png);
            context.Response.ClearContent();
            context.Response.ContentType = "image/png";
            context.Response.BinaryWrite(MStream1.ToArray());

            //image.Dispose();
            MStream.Dispose();
            MStream1.Dispose();
        }

        context.Response.Flush();
        context.Response.End();
    }

    /// <summary>  
    ///               ,    ,    
    ///    ,            
    /// </summary>  
    /// <param name="imgBack">      </param>  
    /// <param name="destImg">       </param>  
    public static Image CombinImage(Image imgBack, string destImg)
    {
        Image img = Image.FromFile(destImg);        //        
        if (img.Height != 65 || img.Width != 65)
        {
            img = KiResizeImage(img, 65, 65, 0);
        }
        Graphics g = Graphics.FromImage(imgBack);

        g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height);      //g.DrawImage(imgBack, 0, 0,    ,    );   

        //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//             

        //g.DrawImage(img,          ,          ,    ,    );  

        g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
        GC.Collect();
        return imgBack;
    }


    /// <summary>  
    /// Resize    
    /// </summary>  
    /// <param name="bmp">  Bitmap</param>  
    /// <param name="newW">    </param>  
    /// <param name="newH">    </param>  
    /// <param name="Mode">   ,    </param>  
    /// <returns>       </returns>  
    public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
    {
        try
        {
            Image b = new Bitmap(newW, newH);
            Graphics g = Graphics.FromImage(b);
            //          
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
            g.Dispose();
            return b;
        }
        catch
        {
            return null;
        }
    }

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

効果は以下の通りです.
ソースDEMO:
http://download.csdn.net/detail/david_520042/6685259
変換元:
http://blog.csdn.net/smartsmile2012/article/details/17119755