二维码生成

#region 二维码生成
/// <summary>
/// 批量生成二维码图片
/// </summary>
[Route("createCodes"), HttpPost]
public List<imageModel> Create_CodeImages(dynamic data)
{
string path = "";
List<imageModel> images = new List<imageModel>();
try
{
//清空目录
DeleteDir(currentPath);
foreach (var item in data)
{
//根据id获取卡片信息
at_asset_card card = _assetcardRepository.Get(Convert.ToString(item));
string jsonStr = "{ \"id\":\"" + card.id + "\",\"card_number\":\"" + card.card_number + "\",\"asset_name\":\"" + card.asset_name + "\",\"specification\":\"" + card.specification + "\"}";
string objectName = card.asset_name + card.card_number;
//生成二维码图片
Bitmap image = Create_QRCode(jsonStr, 6);
//生成自定义图片
path = Create_ImgCodes(image, objectName, "5201314");
imageModel model = new imageModel();
model.path = "/images/code" + path;
model.name = card.asset_name;
model.card_number = card.card_number;
images.Add(model);
}
//打开文件夹
// Open_File(currentPath);
}
catch (Exception ex)
{
throw ex;
}
return images;
}

//程序路径
readonly string currentPath = System.Web.Hosting.HostingEnvironment.MapPath("~/images/code");

/// <summary>
/// 保存图片
/// </summary>
/// <param name="strPath">保存路径</param>
/// <param name="img">图片</param>
public string SaveImg(string strPath, Bitmap img)
{
string path = "";
//保存图片到目录
if (Directory.Exists(strPath))
{
//文件名称
string guid = Guid.NewGuid().ToString().Replace("-", "") + ".png";
img.Save(strPath + "/" + guid, System.Drawing.Imaging.ImageFormat.Png);
path = "/" + guid;
}
else
{
//当前目录不存在,则创建
Directory.CreateDirectory(strPath);
}
return path;
}
/// <summary>
/// 生成二维码图片
/// </summary>
/// <param name="codeImage">文件流</param> 
/// <param name="objectName">字体</param>
/// <returns>二维码图片</returns>
public string Create_ImgCodes(Bitmap codeImage, string objectName, string objectCode)
{
string path = string.Empty;
//设置背景图片
path = System.Web.Hosting.HostingEnvironment.MapPath("~/Templet/temp.png");
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
Bitmap bg = new Bitmap(img);
//为画布bg(图片bg)创建一只画笔
Graphics g = Graphics.FromImage(bg);
//【1】将位图文件codeImage画到画布g上
//【2】codeImage左上角距画布左边界25px、距画布上边界56px
//【3】codeImage的长为原长、宽为原宽
g.DrawImage(codeImage, 60, 30, 150, 150);
g.Dispose();
GC.Collect();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bg.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//将画布bg(图片bg)保存到指定路径
string savePath = SaveImg(currentPath, bg);
return savePath;
}
private Bitmap Create_QRCode(string codeNumber, int size)
{
//创建二维码生成类 
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置编码模式 
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置编码测量度 
qrCodeEncoder.QRCodeScale = size;
//设置编码版本 
qrCodeEncoder.QRCodeVersion = 0;
//设置编码错误纠正 
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
//生成二维码图片 
System.Drawing.Bitmap codeImage = qrCodeEncoder.Encode(codeNumber, Encoding.UTF8);
return codeImage;
}
/// <summary>
/// /打开指定目录
/// </summary>
/// <param name="path"></param>
public void Open_File(string path)
{
System.Diagnostics.Process.Start("explorer.exe", path);
}
/// <summary>
/// 删除目录下所有文件
/// </summary>
/// <param name="aimPath">路径</param>
public void DeleteDir(string aimPath)
{
try
{
//目录是否存在
if (Directory.Exists(aimPath))
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
string[] fileList = Directory.GetFiles(aimPath);
//string[] fileList = Directory.GetFileSystemEntries(aimPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
if (Directory.Exists(file))
{
DeleteDir(aimPath + Path.GetFileName(file));
}
// 否则直接Delete文件
else
{
File.Delete(aimPath + Path.GetFileName(file));
}
}

}
}
catch (Exception e)
{
throw e;
}
}
#endregion

 

posted @ 2019-09-29 10:44  艺洁  阅读(264)  评论(0编辑  收藏  举报