引用using System.Windows.Media.Imaging;
using System;
using System.Net;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Commons
{
/// <summary>
/// 類型轉換集合
/// </summary>
public class TypeConverter
{
/// <summary>
/// 把圖片轉化成二進位
/// </summary>
/// <param name="sourceImage"></param>
/// <returns></returns>
public static byte[] ImageToByteArray(Image sourceImage)
{
BitmapSource bs = sourceImage.Source as BitmapSource;
using (MemoryStream ms = new MemoryStream())
{
System.Windows.Media.Imaging.WriteableBitmap writeableBitmap = new WriteableBitmap(bs);
System.Windows.Media.Imaging.Extensions.SaveJpeg(writeableBitmap, ms, bs.PixelWidth, bs.PixelHeight, 0, 100);
return ms.GetBuffer();
}
}
/// <summary>
/// 把二進位轉化為Image圖片
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
public static Image ByteArrayToImage(byte[] bits)
{
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream ms = new MemoryStream(bits))
{
bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
bitmapImage.SetSource(ms);
Image image = new Image();
image.Source = bitmapImage;
return image;
}
}
}
}