简体   繁体   中英

How to capture a frame of a video using directx

I am fairly new to DirectX and c#I have a new challenge where I am trying to process the frames of video(60 fps) coming as a video stream over HDMI from another PC(using Directx C#). I am using video capture card for capturing video. Moreover my piece of code enables me to capture the video perfectly.

However, I have a requirement where I need to be able to process the frames of video at the same time when it is streaming(may be in a separate thread).

I have tried using AForge library to capture the frames but that only works with the integrated web camera.When I try yo run this with capture card it only shows a black screen Any pointers or links for reference will be really appreciated.

Finally I found the solution myself..The frames of the video stream can be extracted using Directshow's SampleGrabber method.

/// <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 is the link to the Article.Might help some one as it took me a lot of time to get to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM