fast converting Bitmap to BitmapSource wpf

泪湿孤枕 提交于 2019-11-26 02:37:52

问题


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) ?


回答1:


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.

public BitmapSource Convert(System.Drawing.Bitmap bitmap)
{
    var bitmapData = bitmap.LockBits(
        new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

    var bitmapSource = BitmapSource.Create(
        bitmapData.Width, bitmapData.Height,
        bitmap.HorizontalResolution, bitmap.VerticalResolution,
        PixelFormats.Bgr24, null,
        bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);

    bitmap.UnlockBits(bitmapData);
    return bitmapSource;
}



回答2:


I answered myself before Clemens answer with :

[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

 WriteableBitmap writeableBitmap = new WriteableBitmap(1280, 1024, 96.0, 96.0, PixelFormats.Bgr24, null);
        public MainWindow()
        {
            InitializeComponent();

            ImageTarget.Source = writeableBitmap;

            Messenger.Default.Register<Bitmap>(this, (bmp) =>
            {
                ImageTarget.Dispatcher.BeginInvoke((Action)(() =>
               {
                    BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);
                    writeableBitmap.Lock();
                    CopyMemory(writeableBitmap.BackBuffer, data.Scan0,
                        (writeableBitmap.BackBufferStride * bmp.Height));
                    writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, bmp.Width, bmp.Height));
                    writeableBitmap.Unlock();
                    bmp.UnlockBits(data);
                }));
            });
        }

Now my CPU usage is about 15%



来源:https://stackoverflow.com/questions/30727343/fast-converting-bitmap-to-bitmapsource-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!