试试看有没有哪位朋友处理过这样的问题,我使用了一个定时器,每100毫秒获取一个图片的二进制数组,这个数据是由摄像头开发商的接口提供出来的,我只是每隔100毫秒去获取到,然后将这个数据转化为WPF的Image控件可以使用的图片源,具体代码如下:
public UCMeetSysMainTopClient()
{
InitializeComponent();
this.Dispatcher.BeginInvoke(new Action(() =>
{
this._Timer = new DispatcherTimer();
this._Timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
this._Timer.Tick += new EventHandler(_Timer_Tick);
}));
}
void _Timer_Tick(object sender, EventArgs e)
{
//decodeaframe这个方法是由摄像头供应商提供的接口,其中返回的this._byteImageData[0]就是图片二进制数据
if (decodeaframe(ref this._byteImageData[0], ref this._byteCode[0]))
{
//string textData = Encoding.Default.GetString(this._byteCode);
//this.EventAggregator.GetEvent<ScanEvent>().Publish(textData);
}
//问题出现在以下代码
BitmapSource source = ToBitmapSource(CreateBitmap(this._byteImageData, 640, 480));
this.iView.Source = source;
}
private static BitmapSource ToBitmapSource(Bitmap bmp)
{
try
{
return Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
return null;
}
}
private static Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight)
{
Bitmap bitmap = new Bitmap(originalWidth, originalHeight, PixelFormat.Format8bppIndexed);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
stream.Flush();
int num = ((((originalWidth * 8) + 31) / 32) * 4) - originalWidth;
int count = ((((originalWidth * 8) + 31) / 32) * 4) * originalHeight;
int num3 = ReadData(stream, 10, 4);
int num4 = 54;
int num5 = num3;
int num6 = 0;
for (int i = num4; i < num5; i += 4)
{
byte[] buffer = new byte[] { (byte)num6, (byte)num6, (byte)num6, 0 };
num6++;
stream.Position = i;
stream.Write(buffer, 0, 4);