加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

生成二维码的webservice(可以加logo图片)

发布时间:2020-12-17 00:29:07 所属栏目:安全 来源:网络整理
导读:最近搞了个生成二维码的webservice,支持中文,都是利用网上的方法,自己组装了一下。 组件:ThoughtWorks.QRCode.dll webservice: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Prot

最近搞了个生成二维码的webservice,支持中文,都是利用网上的方法,自己组装了一下。

组件:ThoughtWorks.QRCode.dll

webservice:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;

using System.Drawing;
using System.Drawing.Imaging;

using System.IO;


/// <summary>
/// MSGQRCode 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MSGQRCode : System.Web.Services.WebService
{
??? public MSGQRCode()
??? {
??????? //如果使用设计的组件,请取消注释以下行
??????? //InitializeComponent();
??? }
??? [WebMethod]
??? public string CreateQRCode(string url)
??? {
??????? if (string.IsNullOrEmpty(url))
??????? {
??????????? return "";
??????? }
??????? QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
??????? qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
??????? qrCodeEncoder.QRCodeScale = 4;
??????? qrCodeEncoder.QRCodeVersion = 8;
??????? qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
??????? try
??????? {
??????????? String ls_fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".png";
??????????? String ls_savePath = Server.MapPath("../") + "/QRCodeImages/" + ls_fileName;
??????????? qrCodeEncoder.Encode(url,System.Text.Encoding.UTF8).Save(ls_savePath);
??????????? ImageWatermark(Server.MapPath("../QRCodeImages/" + ls_fileName),Server.MapPath("../shuiyin.jpg"),"C");
??????????? return "QRCodeImages/" + ls_fileName;
??????? }
??????? catch (Exception ex)
??????? {
??????????? return ex.Message;
??????? }
??? }
??? /// <summary>
??? /// 图片水印处理方法
??? /// </summary>
??? /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
??? /// <param name="waterpath">水印图片(绝对路径)</param>
??? /// <param name="location">水印位置(传送正确的代码)</param>
??? /// <returns></returns>
??? private string ImageWatermark(string path,string waterpath,string location)
??? {
??????? //获取文件扩展名
??????? string kz_name = Path.GetExtension(path);
??????? //暂时只支持给.JPG,.BMP,.JPEG格式加水印
??????? if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg" || kz_name == ".png")
??????? {
??????????? //设置新的文件名
??????????? DateTime time = DateTime.Now;
??????????? string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
??????????? //加载需要加载水印的图片
??????????? Image img = Bitmap.FromFile(path);
??????????? //加载水印图片
??????????? Image waterimg = Image.FromFile(waterpath);

??????????? //添加水印
??????????? Graphics g = Graphics.FromImage(img);
??????????? //获取水印位置设置
??????????? ArrayList loca = new ArrayList();
??????????? int x = 0;
??????????? int y = 0;
??????????? x = img.Width / 2 - waterimg.Width / 2;
??????????? y = img.Height / 2 - waterimg.Height / 2;
??????????? loca.Add(x);
??????????? loca.Add(y);

??????????? g.DrawImage(waterimg,new Rectangle(int.Parse(loca[0].ToString()),int.Parse(loca[1].ToString()),waterimg.Width,waterimg.Height)); ??????????? //释放资源 ??????????? waterimg.Dispose(); ??????????? g.Dispose(); ??????????? //保存水印图片 ??????????? string newpath = Path.GetDirectoryName(path) + filename + kz_name; ??????????? img.Save(newpath); ??????????? img.Dispose(); ??????????? //将水印复制到原有图片 ??????????? //将水印图片替换原有图片 ??????????? File.Copy(newpath,path,true); ??????????? //删除水印 ??????????? if (File.Exists(newpath)) ??????????? { ??????????????? File.Delete(newpath); ??????????? } ??????? } ??????? return path; ??? } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读