Beispiel #1
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);
        }
Beispiel #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);

            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);
        }
        /// <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);
        }