WPF位图到图像转换仅显示黑色图像

我在以wpf-Imageforms显示图像(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 System.Windows.Controls.Image img = new System.Windows.Controls.Image(); //convert System.Drawing.Image to WPF image System.Drawing.Bitmap 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; 

有谁知道为什么我只看到一个黑色图像(凸轮正在工作!)? 在此先感谢您的帮助


编辑(作为对答案/评论的反应):

我在我的应用程序中使用WPF而不是WinForms。

你是对的,“img”没有使用,因此不正确。 我相应地更改了代码并实现了DeleteObject。 我的代码现在看起来像这样:

 //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); 

与之前的区别在于我看到的是WHITE图像而不是黑色图像。 我不确定为什么它不会让我回到凸轮图像。 是否有任何错误的image1属性或..?

谢谢

我知道这是一段很长的时间,但你的问题可能是因为你的捕获方法( 见下面的行 )你传递的是DontWait,这意味着线程不会等待相机捕获图像,但你会立即访问图像来自相机记忆。 复制图像时,相机可能无法获取图像。

 cam.Acquisition.Capture(uEye.Defines.DeviceParameter.DontWait); 

您可以传入uEye.Defines.DeviceParameter.Wait来阻止线程直到完全捕获图像,或者如果您使用DontWait(我更喜欢这个),请订阅相机OnFrameReceived事件,然后将图像复制到相机中。