欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例

程序员文章站 2023-12-14 00:00:52
本文实例讲述了asp.net(c#)使用qrcode生成图片中心加logo或图像的二维码。分享给大家供大家参考,具体如下: <%@ webhandler l...

本文实例讲述了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>
  /// 调用此函数后使此两种图片合并,类似相册,有个
  /// 背景图,中间贴自己的目标图片
  /// </summary>
  /// <param name="imgback">粘贴的源图片</param>
  /// <param name="destimg">粘贴的目标图片</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, 相框宽, 相框高);
    //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">保留着,暂时未用</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);
      // 插值算法的质量
      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;
    }
  }
}

运行效果如下图所示:

asp.net(C#)使用QRCode生成图片中心加Logo或图像的二维码实例

ps:本站还提供了一个功能十分强悍的在线二维码生成工具,可实现文本、电话号码、短信、邮件、网址等的二维码生成及logo图标添加功能:

在线生成二维码工具(加强版):

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

上一篇:

下一篇: