12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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;
- }
- }
- }
|