好记性不如烂笔头。

ASP.NET 生成二维码 带中心logo图标

ThoughtWorks.QRCode.dll (5.92 mb)下载

QRCodeHandler.ashx 代码如下

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

using System; using System.Web; using ThoughtWorks.QRCode.Codec; using System.Drawing; using System.Drawing.Drawing2D;

public class QRCodeHandler : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    String data = context.Request["url"];
    if (!string.IsNullOrEmpty(data))
    {
        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
        qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
        qrCodeEncoder.QRCodeScale = 2;
        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/favicon.ico")).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();
}

/// &lt;summary&gt;    
/// 调用此函数后使此两种图片合并,类似相册,有个    
/// 背景图,中间贴自己的目标图片    
/// &lt;/summary&gt;    
/// &lt;param name="imgBack"&gt;粘贴的源图片&lt;/param&gt;    
/// &lt;param name="destImg"&gt;粘贴的目标图片&lt;/param&gt;    
public static System.Drawing.Image CombinImage(System.Drawing.Image imgBack, string destImg)
{
    System.Drawing.Image img = System.Drawing.Image.FromFile(destImg);        //照片图片      
    if (img.Height != 65 || img.Width != 65)
    {
        img = KiResizeImage(img, 32, 32, 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;
}


/// &lt;summary&gt;    
/// Resize图片    
/// &lt;/summary&gt;    
/// &lt;param name="bmp"&gt;原始Bitmap&lt;/param&gt;    
/// &lt;param name="newW"&gt;新的宽度&lt;/param&gt;    
/// &lt;param name="newH"&gt;新的高度&lt;/param&gt;    
/// &lt;param name="Mode"&gt;保留着,暂时未用&lt;/param&gt;    
/// &lt;returns&gt;处理以后的图片&lt;/returns&gt;    
public static System.Drawing.Image KiResizeImage(System.Drawing.Image bmp, int newW, int newH, int Mode)
{
    try
    {
        System.Drawing.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;
    }
}

}