【问题标题】:How to capture a frame of a video using directx如何使用directx捕获视频帧
【发布时间】:2015-05-06 11:23:52
【问题描述】:

我对 DirectX 和 c# 相当陌生,我遇到了一个新挑战,我正在尝试处理来自另一台 PC(使用 Directx C#)的视频帧(60 fps)作为视频流通过 HDMI。我正在使用视频采集卡来采集视频。此外,我的一段代码使我能够完美地捕捉视频。

但是,我有一个要求,我需要能够在流式传输的同时处理视频帧(可能在单独的线程中)。

我曾尝试使用 AForge 库 来捕捉帧,但这只适用于集成的网络摄像头。当我尝试使用捕捉卡运行它时,它只显示黑屏 任何供参考的指针或链接将不胜感激。

【问题讨论】:

标签: c# video frames


【解决方案1】:

最后我自己找到了解决方案。视频流的帧可以使用 Directshow 的 SampleGrabber 方法提取。

/// <summary> Interface frame event </summary>
public delegate void HeFrame(System.Drawing.Bitmap BM);
/// <summary> Frame event </summary>
public event HeFrame FrameEvent2;
private    byte[] savedArray;
private    int    bufferedSize;

int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer,
    int BufferLen )
{
    this.bufferedSize = BufferLen;

    int stride = this.SnapShotWidth * 3;

    Marshal.Copy( pBuffer, this.savedArray, 0, BufferLen );

    GCHandle handle = GCHandle.Alloc( this.savedArray, GCHandleType.Pinned );
    int scan0 = (int) handle.AddrOfPinnedObject();
    scan0 += (this.SnapShotHeight - 1) * stride;
    Bitmap b = new Bitmap(this.SnapShotWidth, this.SnapShotHeight, -stride,
        System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr) scan0 );
    handle.Free();
    SetBitmap=b;
    return 0;
}
/// <summary> capture event, triggered by buffer callback. </summary>
private void OnCaptureDone()
{
    Trace.WriteLine( "!!DLG: OnCaptureDone" );
}
/// <summary> Allocate memory space and set SetCallBack </summary>
public void GrapImg()
{
    Trace.Write ("IMG");
    if( this.savedArray == null )
    {
        int size = this.snapShotImageSize;
        if( (size < 1000) || (size > 16000000) )
            return;
        this.savedArray = new byte[ size + 64000 ];
    }
    sampGrabber.SetCallback( this, 1 );
}
/// <summary> Transfer bitmap upon firing event </summary>
public System.Drawing.Bitmap SetBitmap
{
    set
    {
        this.FrameEvent2(value);
    }
}

Here 是文章的链接。可能会对某些人有所帮助,因为我花了很多时间才找到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2013-03-18
    相关资源
    最近更新 更多