Пример #1
0
        private void SetFormat(IntPtr aviStream)
        {
            Avi.BITMAPINFOHEADER bi = new Avi.BITMAPINFOHEADER();
            bi.biSize      = Marshal.SizeOf(bi);
            bi.biWidth     = width;
            bi.biHeight    = height;
            bi.biPlanes    = 1;
            bi.biBitCount  = countBitsPerPixel;
            bi.biSizeImage = frameSize;

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

            if (result != 0)
            {
                throw new Exception("Error in VideoStreamSetFormat: " + result.ToString());
            }
        }
Пример #2
0
        public void GetFrameOpen()
        {
            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
            bih.biBitCount      = countBitsPerPixel;
            bih.biClrImportant  = 0;
            bih.biClrUsed       = 0;
            bih.biCompression   = 0;
            bih.biPlanes        = 1;
            bih.biSize          = Marshal.SizeOf(bih);
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;

            bih.biHeight = 0;
            bih.biWidth  = 0;

            if (bih.biBitCount > 24)
            {
                bih.biBitCount = 32;
            }
            else if (bih.biBitCount > 16)
            {
                bih.biBitCount = 24;
            }
            else if (bih.biBitCount > 8)
            {
                bih.biBitCount = 16;
            }
            else if (bih.biBitCount > 4)
            {
                bih.biBitCount = 8;
            }
            else if (bih.biBitCount > 0)
            {
                bih.biBitCount = 4;
            }

            getFrameObject = Avi.AVIStreamGetFrameOpen(StreamPointer, ref bih);

            if (getFrameObject == 0)
            {
                throw new Exception("Exception in VideoStreamGetFrameOpen!");
            }
        }
Пример #3
0
        public VideoStream(int aviFile, IntPtr aviStream)
        {
            this.aviFile   = aviFile;
            this.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);

            this.frameRate         = (float)streamInfo.dwRate / (float)streamInfo.dwScale;
            this.width             = (int)streamInfo.rcFrame.right;
            this.height            = (int)streamInfo.rcFrame.bottom;
            this.frameSize         = bih.biSizeImage;
            this.countBitsPerPixel = bih.biBitCount;
            this.firstFrame        = Avi.AVIStreamStart(aviStream.ToInt32());
            this.countFrames       = Avi.AVIStreamLength(aviStream.ToInt32());
        }
Пример #4
0
        public Bitmap GetBitmap(int position)
        {
            if (position > countFrames)
            {
                throw new Exception("Invalid frame position: " + position);
            }

            Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

            int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);

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

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

            if (bih.biBitCount < 16)
            {
                bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
            }
            else
            {
                bitmapData = new byte[bih.biSizeImage];
            }

            Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);

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

            Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
            bfh.bfType      = Avi.BMP_MAGIC_COOKIE;
            bfh.bfSize      = (Int32)(55 + bih.biSizeImage);
            bfh.bfReserved1 = 0;
            bfh.bfReserved2 = 0;
            bfh.bfOffBits   = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
            if (bih.biBitCount < 16)
            {
                bfh.bfOffBits += Avi.PALETTE_SIZE;
            }

            BinaryWriter bw = new BinaryWriter(new MemoryStream());

            bw.Write(bfh.bfType);
            bw.Write(bfh.bfSize);
            bw.Write(bfh.bfReserved1);
            bw.Write(bfh.bfReserved2);
            bw.Write(bfh.bfOffBits);
            bw.Write(bitmapInfo);
            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);
        }