//批量打印二维码
public string PrintQRCode()
{
try
{
using (QiMeiEntities dbContext = new QiMeiEntities())
{
string filepath = AppDomain.CurrentDomain.BaseDirectory + "file\\Img\\";
CreatFile(filepath);
var arr = dbContext.Basics_Device.ToList();
if (arr != null)
{
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
foreach (var model in arr)
{
string path = filepath + (model.DeviceName.ToString() + "(" + model.Number.ToString() + ")").Replace("/", "-") + ".jpg";
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
CreateQRImg(model.ID.ToString(), model.DeviceName, model.Number);
}
}
ZipHelper.ZipDirectory(filepath, AppDomain.CurrentDomain.BaseDirectory + "file\\qdcode.zip", "");
}
}
}
catch (Exception ex)
{
LogHelper.Error(this.GetType(), "运行错误:" + ex.Message);
}
return "file\\qdcode.zip";
}
//创建文件夹
public void CreatFile(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (Exception e)
{
LogHelper.Error(this.GetType(), "运行错误:" + e.Message);
}
}
/// <summary>
/// 生成并保存二维码图片的方法
/// </summary>
/// <param name="str">输入的内容</param>
private void CreateQRImg(string str,string name,string code)
{
try
{
Bitmap bt;
string EncoderStr = str;
//生成设置编码实例
QRCodeEncoder QRcode = new QRCodeEncoder();
//设置二维码的规模,默认4
QRcode.QRCodeScale = 4;
//设置二维码的版本,默认7
QRcode.QRCodeVersion = 7;
//设置错误校验级别,默认中等
QRcode.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//设置二维码的背景色
QRcode.QRCodeBackgroundColor = Color.White;
//设置二维码的前景色
QRcode.QRCodeForegroundColor = Color.Black;
//生成二维码图片
bt = QRcode.Encode(EncoderStr, Encoding.UTF8);
//二维码图片的名称
string filename = name + "(" + code + ")";
ImageUtility util = new ImageUtility();
bt = util.InsertWords(new Bitmap(new Bitmap(bt)), filename);
//保存二维码图片在photos路径下
if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "file\\Img\\" + filename + ".jpg"))
{
bt.Save(AppDomain.CurrentDomain.BaseDirectory + "file\\Img\\" + filename.Replace("/","-") + ".jpg");
bt.Dispose();
}
}
catch(Exception ex)
{
LogHelper.Error(this.GetType(), "运行错误:" + ex.Message);
}
}
本文来自博客园,作者:雨到了这里缠成线,转载请注明原文链接:https://www.cnblogs.com/ZhaoMingYuan/p/15955883.html
浙公网安备 33010602011771号