1. 程式人生 > >將二維碼覆在一張背景圖上做宣傳圖

將二維碼覆在一張背景圖上做宣傳圖

今天動手做了一個用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;
        }
    }


}