asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
本文實(shí)例講述了asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼。分享給大家供大家參考,具體如下:
<%@ 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>
/// 調(diào)用此函數(shù)后使此兩種圖片合并,類似相冊,有個(gè)
/// 背景圖,中間貼自己的目標(biāo)圖片
/// </summary>
/// <param name="imgBack">粘貼的源圖片</param>
/// <param name="destImg">粘貼的目標(biāo)圖片</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, 相框?qū)? 相框高);
//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">保留著,暫時(shí)未用</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);
// 插值算法的質(zhì)量
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;
}
}
}
運(yùn)行效果如下圖所示:

PS:本站還提供了一個(gè)功能十分強(qiáng)悍的在線二維碼生成工具,可實(shí)現(xiàn)文本、電話號碼、短信、郵件、網(wǎng)址等的二維碼生成及l(fā)ogo圖標(biāo)添加功能:
在線生成二維碼工具(加強(qiáng)版):
http://tools.jb51.net/transcoding/jb51qrcode
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對大家asp.net程序設(shè)計(jì)有所幫助。
相關(guān)文章
巧用ASP.NET預(yù)編譯Web應(yīng)用程序規(guī)避調(diào)用延遲的方法
ASP.NET 1.x的開發(fā)人員常常聽到用戶抱怨首次調(diào)用應(yīng)用程序的時(shí)候會碰到初始化延遲。畢竟,初次請求會引發(fā)一個(gè)系列過程,包括運(yùn)行庫初始化、分析、把ASPX頁面編譯成中間語言、把方法即時(shí)編譯成本地代碼等等。2011-08-08
asp.ent下合并兩個(gè)結(jié)構(gòu)相同的DataTable
今天遇到了一個(gè)情況,就是從一張數(shù)據(jù)表中讀取幾個(gè)符合條件1的客戶的信息,然后再讀取幾個(gè)符合條件2的客戶的信息,最后顯示出來.因?yàn)榍昂髢纱螖?shù)據(jù)的客戶信息的結(jié)構(gòu)是完全相同的,所以干脆合并成一個(gè)DataTable再賦值給GridView好了.2010-02-02
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問題的解決方法
asp.net 數(shù)據(jù)綁定 使用eval 時(shí)候報(bào) 字符文本中的字符太多 問題解決,需要的朋友可以參考下。2010-09-09
asp.net 刪除MFC單文檔默認(rèn)菜單欄的兩種方法
新建一個(gè)MFC單文檔程序,默認(rèn)都有四個(gè)菜單欄:文件、編輯、視圖和幫助。怎么把這四個(gè)菜單欄刪除掉呢?2010-03-03

