QRコードを1枚の背景図に覆って宣伝図を作る

5105 ワード

今日はaspを使いました.Netは宣伝図の機能を生成して、2枚のピクチャーを用意して、1枚のQRコードのピクチャー1枚の背景図、原理はこのQRコードのピクチャーを背景のピクチャーの上で1枚の新しい図を生成します:
くだらないことを言わずにコードをつけましょう.







    


     <form id="form1" runat="server">  
    <div>  
       
        <button id="Button1" runat="server" text="     "/>  
    </div>  
    <div style="text-align:center">  
        <image id="ImageCode" runat="server" visible="false"/>  
    </div>  
    </form> 


</code></pre> 
  <br/> 
  <pre><code>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 name="newW"/>        
    /// <param name="newH"/>        
    /// <param name="Mode"/>   ,        
    /// <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;
        }
    }


}</code></pre> 
  <br/> 
  <br/> 
   
 </div> 
</div>
                            </div>
                        </div>