http://www.devdiv.com/Windows_Phone-Windows_Phone中BitmapImage转为byte_array,byte_array转为Image控件画到界面中-thread-144736-1-1.html
C#代码
public static byte[] ImageToByteArray(BitmapImage imageSource)
{
using(MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmMap = new WriteableBitmap(imageSource);
// write an image into the stream
Extensions.SaveJpeg(btmMap,ms,imageSource.PixelWidth, imageSource.PixelHeight, 0, 100);
return ms.ToArray();
}
}
public static Image ByteArrayToImage(byte[] bits)
{
BitmapImage btmMap = new BitmapImage();
using (MemoryStream sm = new MemoryStream(bits))
{
btmMap.CreateOptions = BitmapCreateOptions.DelayCreation;
btmMap.SetSource(sm);
Image img = new Image();
img.Source = btmMap;
return img;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
BitmapImage img = new BitmapImage(new Uri("/Image/a.jpg", UriKind.RelativeOrAbsolute));
img.CreateOptions = BitmapCreateOptions.None;
byte[] bits = ImageToByteArray(img);
Image image1 = ByteArrayToImage(bits);
StackPanel1.Children.Add(image1);
}