欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例講述了C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能。分享給大家供大家參考,具體如下:

二維碼早就傳遍大江南北了,總以為它是個(gè)神奇的東西,其實(shí)細(xì)細(xì)研究之后發(fā)現(xiàn)也沒想象的那么神秘,碰巧最近項(xiàng)目中需要?jiǎng)討B(tài)生成二維碼,解決完實(shí)際問題之后,簡單總結(jié)整理一下。項(xiàng)目中除了動(dòng)態(tài)生成二維碼之外,還實(shí)現(xiàn)了動(dòng)態(tài)生成自定義圖片,二維碼可以是其中的元素。

設(shè)置圖片的數(shù)據(jù)源為動(dòng)態(tài)圖片

<body>
  <form id="form1" runat="server" >
  <div>
    <img src="GenerateImage.aspx?type=2" />
  </div>
  </form>
</body>

動(dòng)態(tài)生成圖片

GenerateImage.aspx.cs文件內(nèi)容

protected void Page_Load(object sender, EventArgs e)
{
  string type = Request.QueryString["type"].ToString();
  Bitmap codeImage = Create_QRCode("分享才能獲得更多,我盡力而為(5201314)", 6);
  MemoryStream ms = Create_ImgCode(codeImage, "分享才能獲得更多,我盡力而為", "5201314", type);
  Response.ClearContent();
  Response.ContentType = "image/Png";
  Response.BinaryWrite(ms.ToArray());
  Response.End();
}
private Bitmap Create_QRCode(string codeNumber, int size)
{
  //創(chuàng)建二維碼生成類
  QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
  //設(shè)置編碼模式
  qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
  //設(shè)置編碼測量度
  qrCodeEncoder.QRCodeScale = size;
  //設(shè)置編碼版本
  qrCodeEncoder.QRCodeVersion = 10;
  //設(shè)置編碼錯(cuò)誤糾正
  qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
  //生成二維碼圖片
  System.Drawing.Bitmap codeImage = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8);
  return codeImage;
}
/// <summary>
/// 生成自定義圖片
/// </summary>
/// <param name="codeImage">生成的二維碼</param>
/// <param name="objectName">物體名稱</param>
/// <returns>自定義圖片內(nèi)存流</returns>
private MemoryStream Create_ImgCode(Bitmap codeImage, string objectName, string objectCode, string type)
{
  string path = string.Empty;
  if (type == "1")
  {
    //設(shè)置背景圖片
    path = Server.MapPath("Images/backimg1.png");
  }
  else if (type == "2")
  {
    //設(shè)置背景圖片
    path = Server.MapPath("Images/backimg2.png");
  }
  System.Drawing.Image img = System.Drawing.Image.FromFile(path);
  Bitmap bg = new Bitmap(img);
  //為畫布bg(圖片bg)創(chuàng)建一只畫筆
  Graphics g = Graphics.FromImage(bg);
  if (type == "1")
  {
    //【1】將位圖文件codeImage畫到畫布g上
    //【2】codeImage左上角距畫布左邊界25px、距畫布上邊界56px
    //【3】codeImage的長為原長、寬為原寬
    g.DrawImage(codeImage, 25, 56, codeImage.Width, codeImage.Height);
  }
  else if (type == "2")
  {
    g.DrawImage(codeImage, 132, 19, 162, 162);
    System.Drawing.Brush b = new SolidBrush(Color.Black);
    Font font = new Font("宋體", 8, FontStyle.Regular);
    StringFormat sf = new StringFormat();
    sf.LineAlignment = StringAlignment.Center; // 垂直居中
    sf.Alignment = StringAlignment.Near;    // 水平左對齊
    //string也是畫到畫布上的,當(dāng)畫的string長度大于112px時(shí)會自動(dòng)換行
    SizeF stringSize = g.MeasureString("我的宣言:", font, 112, sf);
    int nWidth = (int)stringSize.Width + 1;
    int nHeight = (int)stringSize.Height + 1;
    RectangleF rf = new Rectangle(new Point(12, 64), new Size(nWidth, nHeight));
    g.DrawString("我的宣言:", font, b, rf, sf);
    stringSize = g.MeasureString(objectName, font, 112, sf);
    int objectWidth = (int)stringSize.Width + 1;
    int objectHeight = (int)stringSize.Height + 1;
    rf = new Rectangle(new Point(12, 64 + nHeight + 8), new Size(objectWidth, objectHeight));
    g.DrawString(objectName, font, b, rf, sf);
    SizeF stringSize1 = g.MeasureString("幸運(yùn)數(shù)字:", font, 112, sf);
    nWidth = (int)stringSize1.Width + 1;
    nHeight = (int)stringSize1.Height + 1;
    RectangleF rf1 = new Rectangle(new Point(12, 136), new Size(nWidth, nHeight));
    g.DrawString("幸運(yùn)數(shù)字:", font, b, rf1, sf);
    stringSize1 = g.MeasureString(objectCode, font, 112, sf);
    objectWidth = (int)stringSize1.Width + 1;
    objectHeight = (int)stringSize1.Height + 1;
    rf1 = new Rectangle(new Point(12, 136 + nHeight + 8), new Size(objectWidth, objectHeight));
    g.DrawString(objectCode, font, b, rf1, sf);
  }
  g.Dispose();
  GC.Collect();
  System.IO.MemoryStream ms = new System.IO.MemoryStream();
  bg.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  //將畫布bg(圖片bg)保存到指定路徑
  path = Server.MapPath("Images");
  bg.Save(path + "\\photoName.png", System.Drawing.Imaging.ImageFormat.Png);
  codeImage.Dispose();
  bg.Dispose();
  return ms;
}

ThoughtWorks.QRCode.dll點(diǎn)擊此處本站下載。

PS:本站還提供了一個(gè)功能十分強(qiáng)悍的在線二維碼生成工具,可實(shí)現(xiàn)文本、電話號碼、短信、郵件、網(wǎng)址等的二維碼生成及l(fā)ogo圖標(biāo)添加功能:

在線生成二維碼工具(加強(qiáng)版):
http://tools.jb51.net/transcoding/jb51qrcode

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。

上一篇:Unity3D實(shí)現(xiàn)播放gif圖功能

欄    目:C#教程

下一篇:Unity3D制作序列幀動(dòng)畫的方法

本文標(biāo)題:C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4900.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有