asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
來(lái)源:易賢網(wǎng) 閱讀:2697 次 日期:2016-08-05 17:00:46
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例”,方便廣大網(wǎng)友查閱!

本文實(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ù)后使此兩種圖片合并,類(lèi)似相冊(cè),有個(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;

    }

  }

}

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話(huà):0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)