今天动手做了一个用asp.net 生成宣传图的功能,需准备两张图片,一张二维码图片一张背景图,原理就是将这张二维码图片覆盖在背景图片上生成一张新图:
不说废话直接上代码吧。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QRCode.aspx.cs" Inherits="test_QRCode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="生成二维码" onclick="Button1_Click" />
</div>
<div style="text-align:center">
<asp:Image ID="ImageCode" runat="server" Visible="false" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test_QRCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//string CodeText = TextBox1.Text.Trim();
//if (!string.IsNullOrEmpty(CodeText))
//{
// ImageCode.Visible = true;
// ImageCode.ImageUrl = "GetQRCode.ashx?CodeText=" + CodeText;
//}
string str_img_bg = Server.MapPath("1.jpg");//背景图片
string str_img_import = Server.MapPath("2.jpg");//导入图片
string str_text = "张三";//导入文字
string str_save_filename = HttpContext.Current.Server.MapPath("test/") + "\\";//处理完成之后保存的文件路径
if (!Directory.Exists(str_save_filename))
Directory.CreateDirectory(str_save_filename);
//加载背景图片
using (System.Drawing.Image pickedImage = new System.Drawing.Bitmap(str_img_bg))
{
using (StreamReader sr = new StreamReader(str_img_import))
{
System.Drawing.Image img_import = System.Drawing.Image.FromStream(sr.BaseStream, true);
if (img_import.Height != 182|| img_import.Width != 182)
{
img_import = KiResizeImage(img_import, 182, 182, 0);
}
//定位
Rectangle fromR = new Rectangle(0, 0, img_import.Width, img_import.Height);
Rectangle toR = new Rectangle(315, 240, img_import.Width, img_import.Height);//嵌入图片的位置 x,y坐标 和 宽高。
using (System.Drawing.Graphics pickedG = System.Drawing.Graphics.FromImage(pickedImage))
{
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//导入图片
pickedG.DrawImage(img_import, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
//导入文字
pickedG.DrawString(str_text, new Font("Microsoft YaHei", 20), new SolidBrush(Color.White), new PointF(320, 460));
//jpg文件输出
ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in icis)
{
if (i.MimeType == "image/jpeg")
{
ici = i;
}
}
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)90);
pickedImage.Save(str_save_filename + "0000.jpg");
}
}
}
}
/// <summary>
/// Resize图片
/// </summary>
/// <param name="bmp">原始Bitmap</param>
/// <param name="newW">新的宽度</param>
/// <param name="newH">新的高度</param>
/// <param name="Mode">保留着,暂时未用</param>
/// <returns>处理以后的图片</returns>
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;
}
}
}