This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Web; | |
using ZXing; | |
using ZXing.QrCode; | |
/// <summary> | |
/// cs_Qrcode_Generate 的摘要描述 | |
/// </summary> | |
public class cs_Qrcode_Generate | |
{ | |
public cs_Qrcode_Generate() | |
{ | |
// | |
// TODO: 在這裡新增建構函式邏輯 | |
// | |
} | |
public string hashString(string s) | |
{ | |
string result=""; | |
return result; | |
} | |
public string Gen_Base64(String s){//字數不可超過300字 | |
string sEncode = "utf-8"; //---或者gb2312 | |
string EncoderContent = GetEncodingString(s,sEncode); | |
if (s.Length > 300) return null; | |
IBarcodeWriter writer = new BarcodeWriter | |
{ | |
Format = BarcodeFormat.QR_CODE, | |
Options = new QrCodeEncodingOptions | |
{ | |
Width = 400, | |
Height = 400 | |
} | |
}; | |
var result = writer.Write(EncoderContent); | |
var bitmap = new Bitmap(result); | |
return Convert.ToBase64String(ImageToByte2(bitmap)); | |
} | |
//轉碼 | |
private string GetEncodingString(string srcString, string sEncode) | |
{ | |
Encoding e8859Encode = Encoding.GetEncoding("iso-8859-1"); | |
Encoding srcEncode = Encoding.Unicode; | |
Encoding dstEncode = Encoding.GetEncoding(sEncode); | |
byte[] srcBytes = srcEncode.GetBytes(srcString); | |
byte[] dstBytes = Encoding.Convert(srcEncode, dstEncode, srcBytes); | |
char[] dstChars = new char[e8859Encode.GetCharCount(dstBytes, 0, dstBytes.Length)]; | |
e8859Encode.GetChars(dstBytes, 0, dstBytes.Length, dstChars, 0); | |
return new string(dstChars); | |
} | |
//bitmap轉byte[] 記憶體 | |
public static byte[] ImageToByte2(Image img) | |
{ | |
byte[] byteArray = new byte[0]; | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); | |
stream.Close(); | |
byteArray = stream.ToArray(); | |
} | |
return byteArray; | |
} | |
} |
沒有留言:
張貼留言