using System.Windows.Media.Imaging; namespace HKW.HKWUtils; /// /// 工具 /// public static class Utils { /// /// 解码像素宽度 /// public const int DecodePixelWidth = 250; /// /// 解码像素高度 /// public const int DecodePixelHeight = 250; public static char[] Separator { get; } = new char[] { '_' }; //public static BitmapImage LoadImageToStream(string imagePath) //{ // BitmapImage bitmapImage = new(); // bitmapImage.BeginInit(); // bitmapImage.DecodePixelWidth = DecodePixelWidth; // try // { // bitmapImage.StreamSource = new StreamReader(imagePath).BaseStream; // } // finally // { // bitmapImage.EndInit(); // } // return bitmapImage; //} /// /// 载入图片至内存流 /// /// 图片路径 /// public static BitmapImage LoadImageToMemoryStream(string imagePath) { BitmapImage bitmapImage = new(); bitmapImage.BeginInit(); try { var bytes = File.ReadAllBytes(imagePath); bitmapImage.StreamSource = new MemoryStream(bytes); bitmapImage.DecodePixelWidth = DecodePixelWidth; } finally { bitmapImage.EndInit(); } return bitmapImage; } /// /// 载入图片至内存流 /// /// 图片流 /// public static BitmapImage LoadImageToMemoryStream(Stream imageStream) { BitmapImage bitmapImage = new(); bitmapImage.BeginInit(); try { bitmapImage.StreamSource = imageStream; bitmapImage.DecodePixelWidth = DecodePixelWidth; } finally { bitmapImage.EndInit(); } return bitmapImage; } }