I need to draw an image on the Image
component at 30Hz.
I use this code :
public MainWindow()
{
InitializeComponent();
Messenger.Default.Register<Bitmap>(this, (bmp) =>
{
ImageTarget.Dispatcher.BeginInvoke((Action)(() =>
{
var hBitmap = bmp.GetHbitmap();
var drawable = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
ImageTarget.Source = drawable;
}));
});
}
The problem is, with this code, My CPU usage is about 80%, and, without the convertion it's about 6%.
So why converting bitmap is so long ?
Are there faster method (with unsafe code) ?
Here is a method that (to my experience) is at least four times faster than
CreateBitmapSourceFromHBitmap
.It requires that you set the correct
PixelFormat
of the resulting BitmapSource.