using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Media.Imaging; namespace vCloudUploaderDemo { internal class BitmapHelper { public static Bitmap BytesToBitmap(byte[] bytes) { if (bytes == null) { return null; } var memory = new MemoryStream(bytes); var bitmap = (Bitmap)Bitmap.FromStream(memory); return bitmap; } public static byte[] BitmapToBytes(Bitmap bitmap) { if (bitmap == null) { return null; } using (var stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Bmp); var buffer = new byte[stream.Length]; stream.Position = 0; stream.Read(buffer, 0, buffer.Length); return buffer; } } public static BitmapImage BytesToBitmapImage(byte[] bytes) { var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(bytes); bitmapImage.EndInit(); return bitmapImage; } public static BitmapImage BitmapToBitmapImage(Bitmap bitmap) { BitmapImage bmpimg = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); bmpimg.BeginInit(); bmpimg.StreamSource = ms; bmpimg.CacheOption = BitmapCacheOption.OnLoad; bmpimg.EndInit(); bmpimg.Freeze(); ms.Dispose(); } return bmpimg; } } }