/// <summary> /// 生成条码 /// </summary> /// <param name="valueCode">条码值</param> /// <param name="width">条码宽</param> /// <param name="herght">条码高</param> /// <param name="format">条码格式</param> /// <returns></returns> public static Bitmap CreatCode(string valueCode, int width, int herght, int format) { try { if (string.IsNullOrEmpty(valueCode)) { valueCode = " "; } EncodingOptions options = new EncodingOptions() { Width = width, Height = herght, }; BarcodeWriter writer = new BarcodeWriter(); //使用ITF 格式,不能被现在常用的支付宝、微信扫出来 //如果想生成可识别的可以使用 CODE_128 格式 //writer.Format = BarcodeFormat.ITF; writer.Format = (BarcodeFormat)format; writer.Options = options; Bitmap map = writer.Write(valueCode.Replace("@@", "+")); return(map); } catch (Exception ex) { HelperLog.Error("生成条码失败!", ex); return(null); } }
/// <summary> /// Post 请求 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="url">完整的url</param> /// <param name="body">post body,可以匿名或者反序列化</param> /// <returns></returns> public async static Task <T> PostApi <T>(string url, object body = null) { try { var client = new RestClient($"{url}"); IRestRequest queest = new RestRequest { Method = Method.POST, }; queest.AddHeader("Accept", "application/json"); queest.AddJsonBody(body); var result = await client.ExecuteAsync(queest); if (result.StatusCode != HttpStatusCode.OK) { return((T)Convert.ChangeType(result.ErrorMessage, typeof(T))); } dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(result.Content, typeof(T)); return((T)temp); } catch (Exception ex) { HelperLog.Error($"POST请求失败!url={url},body={Newtonsoft.Json.JsonConvert.SerializeObject(body)}", ex); } return(default(T)); }
/// <summary> /// 根据配置文件所在文件夹实例化对象 /// </summary> /// <param name="contentPath">配置文件所在文件夹路径</param> public HelperAppsettings(string contentPath) { try { string fileDir = Path.GetDirectoryName(contentPath); string fileName = Path.GetFileName(contentPath); string fileExtension = Path.GetExtension(contentPath); if (string.IsNullOrEmpty(fileExtension) || fileExtension.ToLower() != ".json") { fileDir = Directory.GetCurrentDirectory(); fileName = "appsettings.json"; } else { if (string.IsNullOrEmpty(fileDir)) { fileDir = Directory.GetCurrentDirectory(); } } //如果你把配置文件 是 根据环境变量来分开了,可以这样写 //Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json"; _configuration = new ConfigurationBuilder() .SetBasePath(fileDir) .Add(new JsonConfigurationSource { Path = fileName, Optional = false, ReloadOnChange = true }) //这样的话,可以直接读目录里的json文件,而不是 bin 文件夹下的,所以不用修改复制属性 .Build(); } catch (Exception ex) { HelperLog.Error <HelperAppsettings>($"json配置文件异常!contentPath={contentPath}", ex); } }
/// <summary> /// 获取节点实体 "":"" 或 "" /// </summary> /// <param name="nodeName"></param> /// <returns></returns> public T GetNodeEntity <T>(string nodeName) { try { return(_configuration.GetSection(nodeName).Get <T>()); } catch (Exception ex) { HelperLog.Error <HelperAppsettings>($"获取配置参数失败!nodeName={nodeName}", ex); } return(default(T)); }
/// <summary> /// 获取节点值 "":"" 或 "" /// </summary> /// <param name="nodeName"></param> /// <returns></returns> public string GetNodeValue(string nodeName) { try { return(_configuration.GetSection(nodeName).Get <string>()); } catch (Exception ex) { HelperLog.Error <HelperAppsettings>($"获取配置参数失败!nodeName={nodeName}", ex); } return(string.Empty); }
/// <summary> /// 获取安装的所有打印机名称 /// </summary> /// <returns></returns> public string[] GetPrintNames() { try { var prints = PrinterSettings.InstalledPrinters; string[] strs = new string[prints.Count]; PrinterSettings.InstalledPrinters.CopyTo(strs, 0); } catch (Exception ex) { HelperLog.Error("获取安装所有打印机失败!", ex); } return(new string[] { }); }
public static Bitmap GetBitmap(EntityBitmapLHGQ entity) { Bitmap bitmap = new Bitmap(750, 320); try { Graphics gs = Graphics.FromImage(bitmap); Rectangle mainRec = new Rectangle(new Point(20, 10), new Size(700, 300)); // 左上边框 var rectangle = new Rectangle(mainRec.X + 3, mainRec.Y + 3, 250, 80); gs.DrawRectangle(pen, rectangle); gs.DrawString($"零件名称:{entity.PartName}", font, Brushes.Black, rectangle.X + 3, rectangle.Y + 3); gs.DrawString($"硬件型号:{entity.Hardware}", font, Brushes.Black, rectangle.X + 3, rectangle.Y + 3 + 27); gs.DrawString($"软件版本:{entity.Software}", font, Brushes.Black, rectangle.X + 3, rectangle.Y + 3 + 27 * 2); // 右上边 rectangle = new Rectangle(mainRec.X + 300, mainRec.Y + 3, 250, 80); gs.DrawString(entity.Company, font, Brushes.Black, rectangle.X, rectangle.Y + 3); gs.DrawString(entity.DateTime, font, Brushes.Black, rectangle.X, rectangle.Y + 3 + 27); gs.DrawString(entity.VIN, font, Brushes.Black, rectangle.X, rectangle.Y + 3 + 27 * 2); rectangle = new Rectangle(mainRec.X + 430, mainRec.Y + 35, 44, 44); gs.DrawEllipse(pen, rectangle); gs.DrawString(entity.Sign, font, Brushes.Black, rectangle.X + 22 - entity.Sign.Length * 6, rectangle.Y + 13); gs.DrawString(entity.Num, new Font("华为宋体", 21), Brushes.Black, rectangle.X + 65, rectangle.Y + 15); // 条码 rectangle = new Rectangle(mainRec.X + 8, mainRec.Y + 100, 560, 60); var code = HelperCode.DefaultCode(entity.CodeText); if (code != null) { gs.DrawImage(code, rectangle); } // 虚线及以下 var pneline = new Pen(Color.Black, 1); pneline.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; gs.DrawLine(pneline, mainRec.X + 7, mainRec.Y + 160, mainRec.X + 7 + 500, mainRec.Y + 160); gs.DrawString($"零件号:{entity.PartNum}", font, Brushes.Black, mainRec.X + 180, mainRec.Y + 180); gs.DrawString($"SW:{entity.SW}", font, Brushes.Black, mainRec.X + 70, mainRec.Y + 200); gs.DrawString($"HW:{entity.HW}", font, Brushes.Black, mainRec.X + 310, mainRec.Y + 200); } catch (Exception ex) { HelperLog.Error("将打印内容转成位图时失败!", ex); } return(bitmap); }
/// <summary> /// 打印事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void HelperPrintDocument_PrintPage(object sender, PrintPageEventArgs e) { try { //打印文档 Graphics g = e.Graphics;//获得绘图对象 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.PixelOffsetMode = PixelOffsetMode.Half; g.DrawImage(_bitmap, new Rectangle(1, 1, 900, 250), new Rectangle(1, 1, 1900, 500), GraphicsUnit.Pixel); } catch (Exception ex) { HelperLog.Error("打印失败!", ex); } }
/// <summary> /// 生成二维码 /// </summary> /// <param name="valueCode">条码值</param> /// <param name="mode">编码模式</param> /// <param name="scale">比例</param> /// <param name="version">版本</param> /// <param name="correct">更正</param> /// <returns></returns> public static Bitmap CreatQRCode(string valueCode, int mode, int scale, int version, int correct) { try { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = (QRCodeEncoder.ENCODE_MODE)mode; qrCodeEncoder.QRCodeScale = scale; qrCodeEncoder.QRCodeVersion = version; qrCodeEncoder.QRCodeErrorCorrect = (QRCodeEncoder.ERROR_CORRECTION)correct; Bitmap image = qrCodeEncoder.Encode(valueCode); return(image); } catch (Exception ex) { HelperLog.Error("生成二维码码失败!", ex); return(null); } }
/// <summary> /// Get 请求 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="baseUrl">根域名:http://apk.neters.club/</param> /// <param name="url">接口:api/xx/yy</param> /// <param name="pragm">参数:id=2&name=老张</param> /// <returns></returns> public async static Task <T> GetApi <T>(string url, string pragm = "") { try { var client = new RestClient(url); url = string.IsNullOrEmpty(pragm) ? string.Empty : $"{url}?{pragm}"; var request = await client.ExecuteAsync(new RestRequest(url, Method.GET)); if (request.StatusCode != HttpStatusCode.OK) { return((T)Convert.ChangeType(request.ErrorMessage, typeof(T))); } dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(request.Content, typeof(T)); return((T)temp); } catch (Exception ex) { HelperLog.Error($"Get请求失败!url={url},pragm={pragm}", ex); } return(default(T)); }
/// <summary> /// .net 3.5 版本 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string RequestGet(string url) { string ponseStr = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream receiveStream = response.GetResponseStream()) { StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); ponseStr = readStream.ReadToEnd(); readStream.Close(); receiveStream.Close(); } } catch (Exception ex) { HelperLog.Error($"POST请求失败!url={url}", ex); } return(ponseStr); }
/// <summary> /// .net 3.5 版本 /// </summary> /// <param name="url"></param> /// <param name="body"></param> /// <returns></returns> public static string RequestPost(string url, object body) { string ponseStr = string.Empty; string bodyStr = string.Empty; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = "application/json"; request.Method = "POST"; bodyStr = Newtonsoft.Json.JsonConvert.SerializeObject(body); byte[] bys = System.Text.ASCIIEncoding.ASCII.GetBytes(bodyStr); request.ContentLength = bys.Length; using (Stream requestSream = request.GetRequestStream()) { requestSream.Write(bys, 0, bys.Length); requestSream.Flush(); requestSream.Close(); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream receiveStream = response.GetResponseStream()) { StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); ponseStr = readStream.ReadToEnd(); readStream.Close(); receiveStream.Close(); } } catch (Exception ex) { HelperLog.Error($"POST请求失败!url={url},body={bodyStr}", ex); } return(ponseStr); }