

Ref bufferSize, ref *frameBuffer2, target.Width, target.Height) MediaDet.GetBitmapBits(streamLength * percentagePosition, AllocHGlobal(bufferSize) īyte* frameBuffer2 = (byte*)frameBuffer.ToPointer() equal to mediaDet.GetBitmapBits // (0d, ref bufferSize, ref *buffer, target.Width, target.Height) // allocates enough memory to store the frame IntPtr frameBuffer = get size for buffer int bufferSize = (((s.Width * s.Height) * 24) / 8 ) + bmpinfoheaderSize equal to sizeof(CommonClasses.BITMAPINFOHEADER) int bmpinfoheaderSize = 40 Now we create a bitmap from these bytes (remembering that they start with BITMAPINFOHEADER structure, the size of which is 40 bytes).Ĭopy Code public static Bitmap GetFrameFromVideo( string videoFile,ĭouble percentagePosition, out double streamLength, Size target) Once we have the size of the buffer, we allocate memory on the heap to receive the image (in the first version of this code, memory was allocated on the stack which is fine if the target image is small sized, but if it is big we may get a nice StackOverflowException because stack memory is rather limited).Īfter this, we call GetBitmapBits again, but this time the buffer will be filled with image bytes. The first part is identical to SaveFrameFromVideo, then we have to call GetBitmapBits with the pBuffer parameter set to null to get the size for the buffer of bytes that will contain the 24bpp image ( GetBitmapBits always returns 24bpp images). Using GetBitmapBits to avoid saving the image on disk is a bit trickier, since we need to deal with direct access to memory. (WinStructs.VIDEOINFOHEADER)Marshal.PtrToStructure(mediaType.pbFormat, Their implementation is straight forward:Ĭopy Code private static Size getVideoSize(_AMMediaType mediaType) They are openVideoStream and getVideoSize. You'll notice that two private methods are used here. Throw new InvalidVideoFileException( " No video stream was found") Throw new InvalidVideoFileException(getErrorMsg(( uint)ex.ErrorCode), ex) Target.Width, target.Height, outputBitmapFile) MediaDet.WriteBitmapBits(streamLength * percentagePosition, Target = scaleToFit(target, getVideoSize(mediaType)) calculates the REAL target size of our frame if (target = Size.Empty)

If (openVideoStream(videoFile, out mediaDet, out mediaType)) PercentagePosition, " Valid range is 0.0. Throw new ArgumentOutOfRangeException( " percentagePosition", If (percentagePosition > 1 || percentagePosition < 0)

Copy Code public static void SaveFrameFromVideo( string videoFile,ĭouble percentagePosition, string outputBitmapFile,
