我在以wpf-Image形式显示图像(uEye-Cam)时遇到了一些问题。显示的图像完全是白色的。以下是我用过的代码:
//Get Cam Bitmap Image
var cam = new uEye.Camera();
cam.Init();
cam.Memory.Allocate();
cam.Acquisition.Capture(uEye.Defines.DeviceParameter.DontWait);
Int32 s32MemId;
cam.Memory.GetActive(out s32MemId);
cam.Memory.Lock(s32MemId);
Bitmap bitmap;
cam.Memory.ToBitmap(s32MemId, out bitmap);
//WPF Image control
var image1 = new System.Windows.Controls.Image();
//convert System.Drawing.Image to WPF image
var bmp = new System.Drawing.Bitmap(bitmap);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
image1.Source = wpfBitmap;
image1.Stretch = System.Windows.Media.Stretch.UniformToFill;
image1.Visibility = Visibility.Visible;
DeleteObject(hBitmap);
可能是image1属性有什么问题还是..?
由于
答案 0 :(得分:2)
我在将每个新帧转换为位图方面取得了良好的效果,如问题所示:
Bitmap bmp;
cam.Memory.ToBitmap(s32MemId, out bmp);
然后使用:
将其转换为BitmapImageBitmapImage bitmapImage = null;
using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
{
bitmapImage = new BitmapImage();
bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
然后锁定并冻结(如How do you pass a BitmapImage from a background thread to the UI thread in WPF?中)BitmapImage,使用以下方法将其设置为Image控件的源:
Object tempLock = new Object();
BitmapImage lastBitmapImage = null;
lock (tempLock)
{
lastBitmapImage = bitmapImage.Clone();
}
lastBitmapImage.Freeze();
最后,将BitmapImage设置为Image控件的源:
this.Dispatcher.Invoke(new Action(() =>
{
imageDisplay.Source = lastBitmapImage;
}));