位图到图像的转换仅显示白色图像
本文关键字:图像 显示 白色 位图 转换 | 更新日期: 2023-09-27 18:00:23
我在以wpf图像形式显示图像(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属性可能有什么问题吗。。?
感谢
WPF位图到图像的转换仅显示黑色图像
我在将每个新帧转换为位图方面取得了很好的结果,如问题所示:
Bitmap bmp;
cam.Memory.ToBitmap(s32MemId, out bmp);
然后使用将其转换为位图图像
BitmapImage 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();
}
然后锁定和冻结(如在WPF中如何将BitmapImage从后台线程传递到UI线程?)BitmapImage以使用将其设置为Image控件的源
Object tempLock = new Object();
BitmapImage lastBitmapImage = null;
lock (tempLock)
{
lastBitmapImage = bitmapImage.Clone();
}
lastBitmapImage.Freeze();
最后,BitmapImage被设置为图像控件的来源,带有:
this.Dispatcher.Invoke(new Action(() =>
{
imageDisplay.Source = lastBitmapImage;
}));