using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace IronIntel.Contractor { public class Helper { public static readonly DateTime DBMinDateTime = new DateTime(1900, 01, 01); static Helper() { FileTypes.Add(".jfif", "image/jpeg"); FileTypes.Add(".jpg", "image/jpeg"); FileTypes.Add(".jpeg", "image/jpeg"); FileTypes.Add(".bmp", "image/jpeg"); FileTypes.Add(".gif", "image/jpeg"); FileTypes.Add(".png", "image/png"); FileTypes.Add(".tiff", "image/tiff"); FileTypes.Add(".mp4", "video/mp4"); FileTypes.Add(".mp3", "audio/mpeg"); FileTypes.Add(".mp2", "audio/mp2"); FileTypes.Add(".m4e", "video/mp4"); FileTypes.Add(".mpeg", "video/mpg"); FileTypes.Add(".avi", "video/avi"); FileTypes.Add(".mov", "video/quicktime"); FileTypes.Add(".pdf", "application/pdf"); } public static bool IsNullDateTime(DateTime? dt) { return dt == null || dt.Value < DBMinDateTime; } public static bool IsTrue(string s) { const string YES = "Yes"; const string TRUE = "True"; const string ONE = "1"; return (string.Compare(s, YES, true) == 0) || (string.Compare(s, TRUE, true) == 0) || (string.Compare(s, ONE) == 0); } public static bool Contains(string text, string val) { if (!string.IsNullOrWhiteSpace(text)) { return text.IndexOf(val, StringComparison.OrdinalIgnoreCase) >= 0; } else { return false; } } public static void CloneProperty(object objDes, object objSrc) { if (objDes == null || objSrc == null) return; PropertyInfo[] pisDes = objDes.GetType().GetProperties(); Type tSrc = objSrc.GetType(); foreach (PropertyInfo piDes in pisDes) { if (!piDes.CanWrite) continue; PropertyInfo piSrc = tSrc.GetProperty(piDes.Name, piDes.PropertyType); if (piSrc == null || !piSrc.CanRead) continue; try { piDes.SetValue(objDes, piSrc.GetValue(objSrc, null), null); } catch (Exception e) { e.ToString(); } } } public static object DateValueToNull(DateTime date) { if (date <= DBMinDateTime) return null; else return date; } public static object DateValueToNullOrBigHour(DateTime date) { if (date <= DBMinDateTime) return null; else return date.AddDays(1).AddSeconds(-1); } public static object NumberValueToNull(int value) { if (value < 0) return null; else return value; } public static object NumberValueToNull(double value) { if (value < 0) return null; else return value; } public static string GetDefaultOdoUnitString() { if (SystemParams.CustomerDetail.OdometerUnit == Foresight.Standard.Units.DistanceUnits.Kilometres) return "Kilometre"; else //if (SystemParams.CustomerDetail.OdometerUnit == Foresight.Fleet.Units.DistanceUnits.Miles) return "Mile"; } public static System.Drawing.Color ConvertHtmlColor(string color, System.Drawing.Color def) { System.Drawing.Color c = def; if (System.Text.RegularExpressions.Regex.IsMatch(color, "#([0-9a-fA-F]{6})")) { try { c = System.Drawing.ColorTranslator.FromHtml(color); } catch { } } return c; } public static byte[] GetThumbImg(byte[] buff, int width = 300, int height = 300) { try { using (MemoryStream ms = new MemoryStream(buff)) { ms.Position = 0; System.Drawing.Image img = RotateImage(ms); var tp = Scale(img.Width, img.Height, width, height); System.Drawing.Image img1 = new System.Drawing.Bitmap(img, tp.Item1, tp.Item2); using (MemoryStream ms1 = new MemoryStream()) { img1.Save(ms1, System.Drawing.Imaging.ImageFormat.Png); ms1.Position = 0; return ms1.ToArray(); } } } catch { return buff; } } private static Tuple Scale(int src_width, int src_height, int dest_width, int dest_height) { double srcratio = (double)src_width / (double)src_height; double w = dest_width; double h = (w / srcratio); if (h > dest_height) { h = dest_height; w = h * srcratio; } return new Tuple((int)w, (int)h); } /// /// 根据图片exif调整方向 /// /// /// private static Bitmap RotateImage(Stream sm) { System.Drawing.Image img = System.Drawing.Image.FromStream(sm); var exif = img.PropertyItems; byte orien = 0; var item = exif.Where(m => m.Id == 274).ToArray(); if (item.Length > 0 && item[0].Value != null && item[0].Value.Length > 0) orien = item[0].Value[0]; switch (orien) { case 2: img.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flip break; case 3: img.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-top break; case 4: img.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flip break; case 5: img.RotateFlip(RotateFlipType.Rotate90FlipX); break; case 6: img.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-top break; case 7: img.RotateFlip(RotateFlipType.Rotate270FlipX); break; case 8: img.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom break; default: break; } return (Bitmap)img; } public static bool IsEmail(string email) { //string s = @"^(([^<>()[\]\\.,;:\s@{qm}]+(\.[^<>()[\]\\.,;:\s@{qm}]+)*)|({qm}.+{qm}))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$"; //s = s.Replace("{qm}", "\\\""); string s = @"^\w[-\w.+]*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"; return new System.Text.RegularExpressions.Regex(s).IsMatch(email); } public static bool IsNumber(string str) { string s = @"^\d+$"; return new System.Text.RegularExpressions.Regex(s).IsMatch(str); } public static readonly Dictionary FileTypes = new Dictionary(StringComparer.OrdinalIgnoreCase); public static readonly string[] ImageTypes = new string[] { ".jfif", ".jpg", ".jpeg", ".bmp", ".png", ".tiff", ".gif" }; public static string GetFileExtention(string filename) { if (string.IsNullOrWhiteSpace(filename)) { return ".jpg"; } int idx = filename.LastIndexOf('.'); if (idx > 0 && idx < (filename.Length - 1)) { return filename.Substring(idx); } else { return ".jpg"; } } public static bool IsImage(string filetype) { foreach (var s in ImageTypes) { if (string.Equals(s, filetype, StringComparison.OrdinalIgnoreCase)) { return true; } } return false; } public static double? ConvertToDouble(string d, double? def = null) { if (string.IsNullOrEmpty(d)) { return null; } double t; if (double.TryParse(d, out t)) { return t; } return def; } public static int ConvertToInt32(string d, int def = 0) { int t; if (int.TryParse(d, out t)) { return t; } return def; } public static readonly string[] DevAccounts = new string[] { "dchu@foresightintelligence.com", "qhong@foresightintelligence.com", "lwang@foresightintelligence.com", "ljzeng@foresightintelligence.com", "llj@foresightintelligence.com" }; } }