using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace KHSCALE_TP { public class U3Util { static public Random rnd = new Random(new System.DateTime().Millisecond); static public string GetErrorLogFile(bool bFullPath) { string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")); string AppName = Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf("\\") + 1); string AppTitle = AppName.Substring(0, AppName.LastIndexOf(".")); if (bFullPath) return AppPath + "\\" + AppTitle + "Error.txt"; else return AppTitle + "Error.txt"; } static public string GetEventLogFile(bool bFullPath) { string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")); string AppName = Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf("\\") + 1); string AppTitle = AppName.Substring(0, AppName.LastIndexOf(".")); if (bFullPath) return AppPath + "\\" + AppTitle + "Event.txt"; else return AppTitle + "Event.txt"; } static public string GetImagePath() { string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")); return AppPath + "\\" + "Images"; } public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return destImage; } public static void ErrorLog(string strLog) { try { FileLog(strLog, GetErrorLogFile(false)); } catch { } } public static void EventLog(string strLog) { try { FileLog(strLog, GetEventLogFile(false)); } catch { } } public static void FileLog(string strLog, string strFileName) { string str = ""; DateTime t = DateTime.Now; str = t.ToString("(yyyy-MM-dd HH:mm:ss.fff) "); str += strLog; try { string AppPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")); string strLogFile = AppPath + "\\" + strFileName; FileBackup(strLogFile); FileWrite(strLogFile, str); } catch (Exception ex) { throw new Exception("FileLog->:" + ex.Message); } } public static void FileBackup(string strLogFile) { if (File.Exists(strLogFile)) { try { FileInfo f = new FileInfo(strLogFile); if (f.Length >= (2 * 1024 * 1024)) { System.IO.File.Delete(strLogFile + ".bak"); f.MoveTo(strLogFile + ".bak"); } } catch (Exception ex) { throw new Exception("FileBackup->:" + ex.Message); } } } public static void FileWrite(string strLogFile, string str) { try { FileStream fs = new FileStream(strLogFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); fs.Seek(0, SeekOrigin.End); StreamWriter swFromFile = new StreamWriter(fs); swFromFile.WriteLine(str); swFromFile.Close(); fs.Close(); } catch (Exception ex) { ErrorLog("FileWrite->:" + ex.Message); } } public static string FileRead(string strFileName) { if (System.IO.File.Exists(strFileName) == false) return ""; string result = ""; try { FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); StreamReader srFile = new StreamReader(fs, Encoding.Default, true); result = srFile.ReadToEnd(); srFile.Close(); fs.Close(); } catch (Exception ex) { ErrorLog("FileRead->:" + ex.Message); } return result; } public static bool GetBit(byte b, int bitNumber) { return (b & (1 << bitNumber)) != 0; } public static bool GetBit(short s, int bitNumber) { return (s & (1 << bitNumber)) != 0; } static public DateTime FromUnixTime(long unixTime) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return epoch.AddSeconds(unixTime); } static public Int32 ToUnixTime(DateTime dt) { return (Int32)(dt.Subtract(new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds; } } }