Example #1
0
        /// <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());
        }
Example #2
0
        /*
         *      /// <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);
         *
         *  //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
         *              Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
         *              bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), bih.GetType());
         *
         *              if(bih.biSizeImage < 1){
         *                      throw new Exception("Exception in VideoStreamGetFrame");
         *              }
         *
         *              //copy the image
         *
         *              byte[] bitmapData;
         *              int address = dib + Marshal.SizeOf(bih);
         *              if(bih.biBitCount < 16){
         *                      //copy palette and pixels
         *                      bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
         *              }else{
         *                      //copy only pixels
         *                      bitmapData = new byte[bih.biSizeImage];
         *              }
         *
         *  Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);
         *
         *              //copy bitmap info
         *              byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
         *              IntPtr ptr;
         *              ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
         *              Marshal.StructureToPtr(bih, ptr, false);
         *              address = ptr.ToInt32();
         *  Marshal.Copy(new IntPtr(address), 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.biSizeImage); //size of file as written to disk
         *              bfh.bfReserved1 = 0;
         *              bfh.bfReserved2 = 0;
         *              bfh.bfOffBits = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
         *              if(bih.biBitCount < 16){
         *                      //There is a palette between header and pixel data
         *                      bfh.bfOffBits += 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;
         *      }*/

        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            // Get the pointer to the BITMAPINFOHEADER
            int    firstFrame = Avi.AVIStreamStart(aviStream.ToInt32());
            IntPtr bitmapPtr  = new IntPtr(Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position));

            // Copy into a BITMAPINFOHEADER structure
            Avi.BITMAPINFOHEADER bitmapInfo = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(bitmapPtr, typeof(Avi.BITMAPINFOHEADER));

            // Find the offset of the raw image data
            IntPtr imagePtr = new IntPtr(bitmapPtr.ToInt32() + bitmapInfo.biSize);

            // Determine the stride
            int stride = bitmapInfo.biSizeImage / bitmapInfo.biHeight;

            // Determine the pixel format
            PixelFormat pixelFormat = ConvertBitCountToPixelFormat(bitmapInfo.biBitCount);

            // Create a bitmap from the raw image data
            Bitmap bmp = new Bitmap(bitmapInfo.biWidth, bitmapInfo.biHeight, stride, pixelFormat, imagePtr);

            // Flip the bitmap vertically
            bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

            return(bmp);
        }
Example #3
0
        /// <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)
        {
            base.aviFile   = aviFile;
            base.aviStream = aviStream;

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

            Avi.AVIStreamReadFormat(aviStream, 0, ref bih, ref size);
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(aviStream);

            frameRate         = streamInfo.dwRate / streamInfo.dwScale;
            width             = (int)streamInfo.rcFrame.right;
            height            = (int)streamInfo.rcFrame.bottom;
            frameSize         = bih.biSizeImage;
            countBitsPerPixel = bih.biBitCount;

            int firstFrame = Avi.AVIStreamStart(aviStream.ToInt32());

            countFrames = firstFrame + Avi.AVIStreamLength(aviStream.ToInt32());
        }
Example #4
0
        /// <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(aviStream);

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

            //Copy the bitmap header into a managed struct
            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(ptrDib, bih.GetType());
            //Copy bitmap info header into managed bytes
            byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
            Marshal.Copy(ptrDib, bitmapInfo, 0, bitmapInfo.Length);

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

            //copy the image

            byte[] bitmapData;
            int    address = dib + Marshal.SizeOf(bih);

            if (bih.biBitCount < 16)
            {
                //copy palette and pixels
                bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
            }
            else
            {
                //copy only pixels
                bitmapData = new byte[bih.biSizeImage];
            }

            for (int offset = 0; offset < bitmapData.Length; offset++)
            {
                bitmapData[offset] = Marshal.ReadByte(new IntPtr(address));
                address++;
            }

            //create file header
            Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
            bfh.bfType      = Avi.BMP_MAGIC_COOKIE;
            bfh.bfSize      = (Int32)(55 + bih.biSizeImage);         //size of file as written to disk
            bfh.bfReserved1 = 0;
            bfh.bfReserved2 = 0;
            bfh.bfOffBits   = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
            if (bih.biBitCount < 16)
            {
                //There is a palette between header and pixel data
                bfh.bfOffBits += 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);
        }