/// <summary>Apply a format to a new stream</summary>
        /// <param name="aviStream">The IAVISTREAM</param>
        /// <remarks>
        /// The format must be set before the first frame can be written,
        /// and it cannot be changed later.
        /// </remarks>
        private void SetFormat(IntPtr aviStream, int writePosition)
        {
            Avi.BITMAPINFO bi = new Avi.BITMAPINFO();
            bi.bmiHeader.biWidth     = width;
            bi.bmiHeader.biHeight    = height;
            bi.bmiHeader.biPlanes    = 1;
            bi.bmiHeader.biBitCount  = countBitsPerPixel;
            bi.bmiHeader.biSizeImage = frameSize;
            bi.bmiHeader.biSize      = Marshal.SizeOf(bi.bmiHeader);

            if (countBitsPerPixel < 24)
            {
                bi.bmiHeader.biClrUsed      = this.palette.Length;
                bi.bmiHeader.biClrImportant = this.palette.Length;
                bi.bmiColors = new Avi.RGBQUAD[this.palette.Length];
                this.palette.CopyTo(bi.bmiColors, 0);
                bi.bmiHeader.biSize += bi.bmiColors.Length * Avi.RGBQUAD_SIZE;
            }

            int result = Avi.AVIStreamSetFormat(aviStream, writePosition, ref bi, bi.bmiHeader.biSize);

            if (result != 0)
            {
                throw new Exception("Error in VideoStreamSetFormat: " + result.ToString("X"));
            }
        }
        /// <summary>Initialize a VideoStream for an existing stream</summary>
        /// <param name="aviFile">The file that contains the stream</param>
        /// <param name="aviStream">An IAVISTREAM from [aviFile]</param>
        public VideoStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.aviStream = aviStream;
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            //Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            //int size = Marshal.SizeOf(bih);
            Avi.BITMAPINFO bih  = new Avi.BITMAPINFO();
            int            size = Marshal.SizeOf(bih.bmiHeader);

            Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);

            if (bih.bmiHeader.biBitCount < 24)
            {
                size = Marshal.SizeOf(bih.bmiHeader) + Avi.PALETTE_SIZE;
                Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);
                CopyPalette(bih.bmiColors);
            }

            this.frameRate         = (float)streamInfo.dwRate / (float)streamInfo.dwScale;
            this.width             = (int)streamInfo.rcFrame.right;
            this.height            = (int)streamInfo.rcFrame.bottom;
            this.frameSize         = bih.bmiHeader.biSizeImage;
            this.countBitsPerPixel = bih.bmiHeader.biBitCount;
            this.firstFrame        = Avi.AVIStreamStart(aviStream.ToInt32());
            this.countFrames       = Avi.AVIStreamLength(aviStream.ToInt32());
        }
        /// <summary>Export a frame into a bitmap</summary>
        /// <param name="position">Position of the frame</param>
        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            Avi.BITMAPINFO bih        = new Avi.BITMAPINFO();
            int            headerSize = Marshal.SizeOf(bih.bmiHeader);

            //Decompress the frame and return a pointer to the DIB
            int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

            //Copy the bitmap header into a managed struct
            bih.bmiColors = this.palette;
            bih.bmiHeader = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), bih.bmiHeader.GetType());

            if (bih.bmiHeader.biSizeImage < 1)
            {
                throw new Exception("Exception in VideoStreamGetFrame");
            }

            //copy the image
            int framePaletteSize = bih.bmiHeader.biClrUsed * Avi.RGBQUAD_SIZE;

            byte[] bitmapData = new byte[bih.bmiHeader.biSizeImage];
            IntPtr dibPointer = new IntPtr(dib + Marshal.SizeOf(bih.bmiHeader) + framePaletteSize);

            Marshal.Copy(dibPointer, bitmapData, 0, bih.bmiHeader.biSizeImage);

            //copy bitmap info
            byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
            IntPtr ptr        = Marshal.AllocHGlobal(bitmapInfo.Length);

            Marshal.StructureToPtr(bih, ptr, false);
            Marshal.Copy(ptr, bitmapInfo, 0, bitmapInfo.Length);
            Marshal.FreeHGlobal(ptr);

            //create file header
            Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
            bfh.bfType      = Avi.BMP_MAGIC_COOKIE;
            bfh.bfSize      = (Int32)(55 + bih.bmiHeader.biSizeImage);        //size of file as written to disk
            bfh.bfReserved1 = 0;
            bfh.bfReserved2 = 0;
            bfh.bfOffBits   = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
            if (bih.bmiHeader.biBitCount < 8)
            {
                //There is a palette between header and pixel data
                bfh.bfOffBits += bih.bmiHeader.biClrUsed * Avi.RGBQUAD_SIZE; //Avi.PALETTE_SIZE;
            }

            //write a bitmap stream
            BinaryWriter bw = new BinaryWriter(new MemoryStream());

            //write header
            bw.Write(bfh.bfType);
            bw.Write(bfh.bfSize);
            bw.Write(bfh.bfReserved1);
            bw.Write(bfh.bfReserved2);
            bw.Write(bfh.bfOffBits);
            //write bitmap info
            bw.Write(bitmapInfo);
            //write bitmap data
            bw.Write(bitmapData);

            Bitmap   bmp            = (Bitmap)Image.FromStream(bw.BaseStream);
            Bitmap   saveableBitmap = new Bitmap(bmp.Width, bmp.Height);
            Graphics g = Graphics.FromImage(saveableBitmap);

            g.DrawImage(bmp, 0, 0);
            g.Dispose();
            bmp.Dispose();

            bw.Close();
            return(saveableBitmap);
        }