Dela via


MediaFrameReader.TryAcquireLatestFrame Method

Definition

Attempts to obtain a MediaFrameReference object representing the latest frame from the MediaFrameSource.

public:
 virtual MediaFrameReference ^ TryAcquireLatestFrame() = TryAcquireLatestFrame;
MediaFrameReference TryAcquireLatestFrame();
public MediaFrameReference TryAcquireLatestFrame();
function tryAcquireLatestFrame()
Public Function TryAcquireLatestFrame () As MediaFrameReference

Returns

A MediaFrameReference representing the latest frame from the media frame source.

Remarks

This method does not always succeed. If the method fails, the returned MediaFrameReference will be null, so you should always test to see if the returned object is null before trying to use it. You should call this method from within the handler for the MediaFrameReader.FrameArrived event instead of manually polling for new frames.

Each MediaFrameReader maintains a circular buffer of MediaFrameReference objects obtained from TryAcquireLatestFrame. After all of the MediaFrameReference objects in the buffer have been used, subsequent calls to TryAcquireLatestFrame will cause the system to call Close (or Dispose in C#) on the oldest buffer object in order to reuse it. Once the object has been disposed, you can no longer use it to access the frame data. For this reason, you should not store the MediaFrameReference object longer than you actually need to process the frame. If your app scenario requires you to keep a reference to the data for longer, you should use one of the APIs provided to get the underlying data. These include:

It is the responsibility of the app to call Close (or Dispose) on the objects returned by these APIs when they are no longer being used.

Important

If you access the SoftwareBitmap or Direct3DSurface objects provided by the VideoMediaFrame property of a MediaFrameReference, the system creates a strong reference to these objects, which means that they will not be disposed when you call Dispose on the containing MediaFrameReference. This also applies to the SoftwareBitmap or Direct3DSurface objects provided by the VideoFrame object that you obtain from the MediaFrameReference.VideoMediaFrame property. You must explicitly call the Dispose method of the SoftwareBitmap or Direct3DSurface directly for the objects to be immediately disposed. Otherwise, the garbage collector will eventually free the memory for these objects, but you can't know when this will occur, and if the number of allocated bitmaps or surfaces exceeds the maximum amount allowed by the system, the flow of new frames will stop.

Applies to