Beispiel #1
0
        /// <summary>Exports a frame into a bitmap file</summary>
        /// <param name="position">Position of the frame</param>
        /// <param name="dstFileName">Name ofthe file to store the bitmap</param>
        public void ExportBitmap(int position, String dstFileName)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position");
            }

            //Decompress the frame and return a pointer to the DIB
            int pDib = VFW.AVIStreamGetFrame(getFrameObject, firstFrame + position);
            //Copy the bitmap header into a managed struct
            BITMAPINFOHEADER bih = new BITMAPINFOHEADER();

            bih = (BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(pDib), bih.GetType());

            /*if(bih.biBitCount < 24){
             *  throw new Exception("Not enough colors! DIB color depth is less than 24 bit.");
             * }else */
            if (bih.biSizeImage < 1)
            {
                throw new Exception("Exception in AVIStreamGetFrame: Not bitmap decompressed.");
            }

            //Copy the image
            byte[] bitmapData = new byte[bih.biSizeImage];
            int    address    = pDib + Marshal.SizeOf(bih);

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

            //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();
            for (int offset = 0; offset < bitmapInfo.Length; offset++)
            {
                bitmapInfo[offset] = Marshal.ReadByte(new IntPtr(address));
                address++;
            }

            //Create file header
            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();

            bfh.bfType      = Util.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);

            //Create or overwrite the destination file
            FileStream   fs = new FileStream(dstFileName, System.IO.FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            //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);
            bw.Close();
            fs.Close();
        }
Beispiel #2
0
        /// <summary>Exports a frame into a bitmap file</summary>
        /// <param name="position">Position of the frame</param>
        /// <param name="dstFileName">Name ofthe file to store the bitmap</param>
        public void ExportBitmap( int position, String dstFileName ) {
            if ( position > countFrames ) {
                throw new Exception( "Invalid frame position" );
            }

            //Decompress the frame and return a pointer to the DIB
            int pDib = VFW.AVIStreamGetFrame( getFrameObject, firstFrame + position );
            //Copy the bitmap header into a managed struct
            BITMAPINFOHEADER bih = new BITMAPINFOHEADER();
            bih = (BITMAPINFOHEADER)Marshal.PtrToStructure( new IntPtr( pDib ), bih.GetType() );

            /*if(bih.biBitCount < 24){
                throw new Exception("Not enough colors! DIB color depth is less than 24 bit.");
            }else */
            if ( bih.biSizeImage < 1 ) {
                throw new Exception( "Exception in AVIStreamGetFrame: Not bitmap decompressed." );
            }

            //Copy the image
            byte[] bitmapData = new byte[bih.biSizeImage];
            int address = pDib + Marshal.SizeOf( bih );
            for ( int offset = 0; offset < bitmapData.Length; offset++ ) {
                bitmapData[offset] = Marshal.ReadByte( new IntPtr( address ) );
                address++;
            }

            //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();
            for ( int offset = 0; offset < bitmapInfo.Length; offset++ ) {
                bitmapInfo[offset] = Marshal.ReadByte( new IntPtr( address ) );
                address++;
            }

            //Create file header
            BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
            bfh.bfType = Util.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 );

            //Create or overwrite the destination file
            FileStream fs = new FileStream( dstFileName, System.IO.FileMode.Create );
            BinaryWriter bw = new BinaryWriter( fs );

            //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 );
            bw.Close();
            fs.Close();
        }