using System.IO; using System.Security.Cryptography; using System.Text; using System.Windows.Media.Imaging; namespace Hotel_Course_Project { public class SomeMethods { #region Конвертирование байтового изображения в стандарт public static BitmapImage ConvertByteToBitmapImage(byte[] array) { if (TestconvertBitToImage(array) == true) { using (var ms = new System.IO.MemoryStream(array)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); return image; } } else { return null; } } public static bool TestconvertBitToImage(byte[] array) { try { using (var ms = new System.IO.MemoryStream(array)) { var image = new BitmapImage(); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = ms; image.EndInit(); } return true; } catch { return false; } } #endregion #region Конвертирование стандарт изображения в массив байта public static byte[] ConvertBitmapImageToByte(BitmapSource image) { using (var stream = new MemoryStream()) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(image)); encoder.Save(stream); return stream.ToArray(); } } #endregion #region Хеширование public static string Hash(string password) { //переводим строку в байт-массим byte[] bytes = Encoding.Unicode.GetBytes(password); //создаем объект для получения средст шифрования MD5CryptoServiceProvider CSP = new MD5CryptoServiceProvider(); //вычисляем хеш-представление в байтах byte[] byteHash = CSP.ComputeHash(bytes); //создаем пустую строку string hash = string.Empty; //формируем одну цельную строку из массива foreach (byte b in byteHash) { hash += string.Format("{0:x2}", b); } return hash; } #endregion public static string pass; } }