如何从像素字节数组创建BitmapImage(实时视频显示)

7
我需要在一个WPF控件上显示实时图像。 我正在寻找使用WPF最快的方法来实现这一点。
我正在使用摄像头的dll API(AVT)捕获图像。
图像由dll编写,相机通过指向名为tFrame的Image结构的IntPtr引发回调(如下所述)。 像素数据存储在ImageBuffer属性中,该属性是指向字节数组的InPtr。
我知道如何从像素字节数组创建位图,但不知道如何创建BitmapImage。 所以可以创建位图,然后从中创建BitmapImage。 这里有一种从内存中的位图创建BitmapImage的方法。 但我想直接从数据源(tFrame)创建BitmapImage。 我该怎么做?
我知道BitmapImage具有CopyPixels方法,但缺少SetPixels。
public struct tFrame
{
    public IntPtr AncillaryBuffer;
    public uint AncillaryBufferSize;
    public uint AncillarySize;
    public tBayerPattern BayerPattern;
    public uint BitDepth;
    public tFrameCtx Context;
    public tImageFormat Format;
    public uint FrameCount;
    public uint Height;
    public IntPtr ImageBuffer;
    public uint ImageBufferSize;
    public uint ImageSize;
    public uint RegionX;
    public uint RegionY;
    public tErr Status;
    public uint TimestampHi;
    public uint TimestampLo;
    public uint Width;
}

这里是我从像素字节数组创建位图的方法。这个方法用于软件的WinForm版本。
private void CreateBitmap(tFrame frame)
{
    //This sample is for a 8bpp captured image
    PixelFormat pxFormat = PixelFormat.Format8bppIndexed;

    //STRIDE
    //[https://dev59.com/SUvSa4cB1Zd3GeqPbRG6#1983886][3]
    //float bitsPerPixel = System.Drawing.Image.GetPixelFormatSize(format);
    int bitsPerPixel = ((int)pxFormat >> 8) & 0xFF;
    //Number of bits used to store the image data per line (only the valid data)
    int validBitsPerLine = ((int)frame.Width) * bitsPerPixel;
    //4 bytes for every int32 (32 bits)
    int stride = ((validBitsPerLine + 31) / 32) * 4;

    Bitmap bmp = new Bitmap((int)frame.Width, (int)frame.Height, stride, pxFormat, frame.ImageBuffer);
}

编辑1:

感谢dr.mo,现在我能够显示60FPS 1024x1024的图像,并且CPU使用率仅为~3%!我的做法是:

//@ UI Thread
public WriteableBitmap wbm = new WriteableBitmap(1024, 1024, (double)96, (double)96, System.Windows.Media.PixelFormats.Gray8, null);
this.wbBackBuffer = this.wbm.BackBuffer;

//This can be called by a timer in the UI thread or at the grab Thread for every image, the CPU usage is almost the same.
void UpdateDisplayImage()
{
wbm.Lock();
wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight));
wbm.Unlock();
}

//@ Grab Thread
//Update the backbuffer with new camera image data.
UpdateBackBuffer(...);

/// <summary>
/// [http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx]
/// </summary>
public void UpdateBackBuffer(IntPtr pData, int w, int h, int ch)
{
    //Can not acess wbm from outside UI thread
    //CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch));
    //I dont know if it is safe to write to it buffer like this:
    CopyMemory(this.wbBackBuffer, pData, (uint)(w * h * ch));
}

msdn.microsoft.com/en-gb/library/system.windows.interop.imaging.createbitmapsourcefrommemorysection.aspx - DarkSquirrel42
请查看此链接:https://dev59.com/BW_Xa4cB1Zd3GeqP0Fkg#15290190 - Obama
@ DarkSquirrel42。抱歉,该链接已损坏。 - Pedro77
1个回答

16

这应该能解决问题。它非常快速。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Drawing;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;


public class MakeBitmapSource
{
    [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
    public static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);



    public static BitmapSource FromNativePointer(IntPtr pData, int w, int h, int ch)
    {
        PixelFormat format = PixelFormats.Default;

        if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
        if (ch == 3) format = PixelFormats.Bgr24; //RGB
        if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha


        WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
        CopyMemory(wbm.BackBuffer, pData, (uint)(w * h * ch));

        wbm.Lock();
        wbm.AddDirtyRect(new Int32Rect(0, 0, wbm.PixelWidth, wbm.PixelHeight));
        wbm.Unlock();

        return wbm;
    }

    public static BitmapSource FromArray(byte[] data, int w, int h, int ch)
    {
        PixelFormat format = PixelFormats.Default;

        if (ch == 1) format = PixelFormats.Gray8; //grey scale image 0-255
        if (ch == 3) format = PixelFormats.Bgr24; //RGB
        if (ch == 4) format = PixelFormats.Bgr32; //RGB + alpha


        WriteableBitmap wbm = new WriteableBitmap(w, h, 96, 96, format, null);
        wbm.WritePixels(new Int32Rect(0, 0, w, h), data, ch * w, 0);

        return wbm;
    }
}

你也可以在非UI线程更新后备缓冲区! 只需确保冻结wbm,并在完成后从UI线程调用wbm.AddDirtyRect。我使用这个来显示网络摄像头视频。效果很棒。 - morishuz
非常好!你尝试过这个解决方案吗:https://dev59.com/BW_Xa4cB1Zd3GeqP0Fkg#15290190?它也会返回一个BitmapSource。我不知道它是否比你的解决方案更快。 - Pedro77
1
没有,但我知道它肯定会更慢,因为它需要在每一步创建图像。使用 WB,你只需要复制数据而不是每次更新视频帧都创建新的图像。此外,问题在于:1. 你没有一个字节数组,而是一个本地指针(IntPtr);2. 你可以使用 WB 在非 UI 线程中进行更新——这对你所尝试的操作至关重要。这是我发现的在 WPF 中显示位图的绝对最快的方法。 - morishuz
1
@Erti-Chris Eelmaa 是的,一旦WB被分配到源后,您可以更新它!您所要做的就是冻结WB,获取对其本机BackBuffer属性的引用,并在任何需要的地方更新它,包括非UI线程。然而,除非您在UI线程中调用AddDirtyRect函数,否则图像不会更新。就是这样。 - morishuz
是的,我刚刚做了那个,但如果你冻结了WB,你就不能使用AddDirtRect更新它,因为会引发异常。我的WB没有被冻结,我通过锁定、添加脏矩形和解锁来更新缓冲区IntPtr副本并更新屏幕。 - Pedro77
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接