using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; namespace Bro.Common.Helper { public static class StaticHelper { /// /// 数值转换为byte数组 高位在前,低位在后 /// /// /// /// public static byte[] IntToBytes(this int number, int size = 2) { byte[] result = new byte[size]; int temp = size; while (temp > 0) { result[size - temp] = (byte)(number >> ((temp - 1) * 8) & 0xff); temp--; } return result; } /// /// 字节数组转换为整数 /// /// 字节数组 /// true:数组序号低的在高位 false:数组序号低的在低位 /// public static int BytesToInt(this byte[] data, bool HtL = true) { int res = 0; for (int i = 0; i < data.Length; i++) { int index = i; if (HtL) { index = data.Length - 1 - i; } res += data[index] << (8 * i); } return res; } /// /// 将32位整形拆分为无符号16位整形 /// /// 需要拆分的32位整形 /// 拆分为16位整形的位数 1或者2 /// true:高位在前,低位在后;false:高位在后,低位在前 /// public static List ParseIntToUnsignShortList(this int num, int bitNum = 2, bool HtL = false) { if (bitNum == 2) { ushort high = (ushort)(num >> 16); ushort low = (ushort)num; if (HtL) { return new List() { high, low }; } else { return new List() { low, high }; } } else { if (num < 0) { num = ushort.MaxValue + 1 + num; } return new List() { (ushort)num }; } } /// /// 将32位整形数组拆分为无符号16位整形数组 /// /// 需要拆分的32位整形 /// 拆分为16位整形的位数 1或者2 /// true:高位在前,低位在后;false:高位在后,低位在前 /// public static List ParseIntToUnsignShortList(this List list, int bitNum = 2, bool HtL = false) { return list.SelectMany(u => u.ParseIntToUnsignShortList(bitNum, HtL)).ToList(); } /// /// 将ushort的集合转换为16位带符号整形 /// /// /// 合并的位数 1或者2 /// true:高位在前,低位在后;false:高位在后,低位在前 /// public static List ParseUnsignShortListToInt(this List numList, int bitNum = 2, bool HtL = false) { if (bitNum == 1) { return numList.ConvertAll(n => { int num = n; if (num > short.MaxValue) { num = num - ushort.MaxValue - 1; } return num; }); } else { List list = new List(); for (int i = 0; i < numList.Count; i += 2) { int high = HtL ? numList[i] : numList[i + 1]; int low = HtL ? numList[i + 1] : numList[i]; list.Add((high << 16) | low); } return list; } } public static string GetExceptionMessage(this Exception ex) { string msg = "异常信息:" + ex.Message; if (ex.InnerException != null) { msg += ";\r\n内部异常信息:" + ex.InnerException.GetExceptionMessage(); } msg += (";\r\nStackTrace:" + ex.StackTrace); return msg; } public static T DeepSerializeClone(this T t) { return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(t)); } public static void DataFrom(this T1 destT, T2 sourceT, List exceptionProps = null) where T1 : class where T2 : class { if (sourceT == null) { destT = null; return; } PropertyInfo[] propDest = destT.GetType().GetProperties();//.Where(p => !(p.GetMethod.IsVirtual && !p.GetMethod.IsFinal)).ToArray(); PropertyInfo[] propSource = sourceT.GetType().GetProperties(); Array.ForEach(propDest, prop => { if (exceptionProps == null || !exceptionProps.Contains(prop.Name)) { if (prop.CanWrite) { PropertyInfo propS = propSource.FirstOrDefault(p => p.Name == prop.Name); if (propS != null && propS.CanRead) { prop.SetValue(destT, propS.GetValue(sourceT)); } } } }); } public static Bitmap BitmapSerializeCopy(this Bitmap map) { Bitmap image = null; using (MemoryStream ms = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, map); ms.Seek(0, SeekOrigin.Begin); image = (Bitmap)bf.Deserialize(ms); //ms.Close(); } return image; //Bitmap image = new Bitmap(map.Width, map.Height); //int bitdepth_per_pixel = Bitmap.GetPixelFormatSize(map.PixelFormat) / 8; //BitmapData source_bitmapdata = null; //BitmapData destination_bitmapdata = null; //try //{ // source_bitmapdata = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, map.PixelFormat); // destination_bitmapdata = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat); // int source_bitmapdata_bitdepth_width = source_bitmapdata.Width * bitdepth_per_pixel; // int source_bitmapdata_height = source_bitmapdata.Height; // int source_bitmapdata_bitdepth_stride = source_bitmapdata.Stride; // unsafe // { // byte* source_ptr = (byte*)source_bitmapdata.Scan0; // byte* destination_ptr = (byte*)destination_bitmapdata.Scan0; // int offset = source_bitmapdata_bitdepth_stride - source_bitmapdata_bitdepth_width; // for (int i = 0; i < source_bitmapdata_height; i++) // { // for (int j = 0; j < source_bitmapdata_bitdepth_width; j++, source_ptr++, destination_ptr++) // { // *destination_ptr = *source_ptr; // } // source_ptr += offset; // destination_ptr += offset; // } // } // map.UnlockBits(source_bitmapdata); // image.UnlockBits(destination_bitmapdata); //} //catch { } //return image; } public static void Sort(this ObservableCollection collection) where T : IComparable { List sortedList = collection.OrderByDescending(x => x).ToList();//这里用降序 for (int i = 0; i < sortedList.Count(); i++) { collection.Move(collection.IndexOf(sortedList[i]), i); } } // 布尔类型转换为整型 public static int ToInt(this object obj) { if (Convert.ToBoolean(obj) == true) return 1; else return 0; } // 整型转换为布尔类型 public static bool ToBool(this object obj) { if (Convert.ToInt32(obj) == 1) return true; else return false; } } }