一、BitmapImage对象和byte[]之间的互转
参考了https://blog.csdn.net/gooapple/article/details/50616821
/// <summary>
/// byte[]转为BitmapImage
/// </summary>
/// <param name="byteArray"></param>
/// <returns></returns>
public static BitmapImage ToImage(byte[] byteArray)
{
BitmapImage bmp = null;
try
{
bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = new MemoryStream(byteArray);
bmp.EndInit();
}
catch
{
bmp = null;
}
return bmp;
}
/// <summary>
/// BitmapImage转为byte[]
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static byte[] ToByteArray(BitmapImage bmp)
{
byte[] ByteArray = null;
try
{
Stream stream = bmp.StreamSource;
if (stream != null && stream.Length > 0)
{
stream.Position = 0;
using (BinaryReader br = new BinaryReader(stream))
{
ByteArray = br.ReadBytes((int)stream.Length);
}
}
}
catch
{
return null;
}
return ByteArray;
}
二、 BitmapImage和Bitmap互换
//将Bitmap对象转换成bitmapImage对象
public BitmapImage ConvertBitmapToBitmapImage(Bitmap bitmap)
{
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
image.StreamSource = stream;
image.EndInit();
return image;
}
//将bitmapImage对象转换成Bitmap对象
public static System.Drawing.Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
return bitmap;
}
}
三、实际应用
1、不能用的代码,会遇到调用线程无法访问此对象,因为另一个线程拥有该对象,至今我还不清楚(应该不是 Dispatcher.BeginInvoke 更新UI的问题,可能是两个using的问题)。
代码如下:
private void video_NewFrame(object sender, Video.NewFrameEventArgs eventArgs)
{
try
{
BitmapImage bi;
Byte[] Array;
using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
{
bi = bitmap.ToBitmapImage();
//将BitmapImage转成字节
Stream stream = bi.StreamSource;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
Array = br.ReadBytes((int)stream.Length);
}
}
}
bi.Freeze(); // avoid cross thread operations and prevents leaks
Dispatcher.BeginInvoke(new ThreadStart(delegate { videoPlayer.Source = bi; }));
}
catch (Exception exc)
{
MessageBox.Show("Error on _videoSource_NewFrame:\n" + exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
StopCamera();
}
}
2、去掉里面的using,正常运行
private void video_NewFrame(object sender, Video.NewFrameEventArgs eventArgs)
{
try
{
BitmapImage bi;
Byte[] Array;
using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
{
bi = bitmap.ToBitmapImage();
//将BitmapImage转成字节
Stream stream = bi.StreamSource;
if (stream != null && stream.Length > 0)
{
BinaryReader br = new BinaryReader(stream);
Array = br.ReadBytes((int)stream.Length);
}
}
bi.Freeze(); // avoid cross thread operations and prevents leaks
Dispatcher.BeginInvoke(new ThreadStart(delegate { videoPlayer.Source = bi; }));
}
catch (Exception exc)
{
MessageBox.Show("Error on _videoSource_NewFrame:\n" + exc.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
StopCamera();
}
}