Exemplo n.º 1
0
        /// <summary>
        /// Gets the current frame from the buffer.
        /// </summary>
        /// <returns>The Bitmap of the frame.</returns>
        public IDisposable GetFrame(IBitmapLoader BitmapLoader)
        {
            if (_actualGraphState != GraphState.Rendered)
            {
                return(null);
            }

            //Asks for the buffer size.
            var bufferSize = 0;

            _sampGrabber.GetCurrentBuffer(ref bufferSize, IntPtr.Zero);

            if (bufferSize <= 0)
            {
                return(null);
            }

            if (_savedArray == null || _savedArray.Length < bufferSize)
            {
                _savedArray = new byte[bufferSize + 64000];
            }

            //Allocs the byte array.
            var handleObj = GCHandle.Alloc(_savedArray, GCHandleType.Pinned);

            //Gets the addres of the pinned object.
            var address = handleObj.AddrOfPinnedObject();

            try
            {
                //Puts the buffer inside the byte array.
                _sampGrabber.GetCurrentBuffer(ref bufferSize, address);

                //Image size.
                var width  = _videoInfoHeader.BmiHeader.Width;
                var height = _videoInfoHeader.BmiHeader.Height;

                var stride = width * 4;
                address += height * stride;

                return(BitmapLoader.CreateBitmapBgr32(new Size(width, height), address, -stride));
            }
            finally
            {
                handleObj.Free();
            }
        }