Ejemplo n.º 1
0
        //创建一个AVI文件
        public void Open(string fname, int width, int height)
        {
            // 关闭当前文件
            Close();

            // 计算跨度
            stride = width * 3;
            int r = stride % 4;

            if (r != 0)
            {
                stride += (4 - r);
            }

            // 创建一个新文件
            if (Win32.AVIFileOpen(out file, fname, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero) != 0)
            {
                throw new ApplicationException("打开文件失败!");
            }

            this.width  = width;
            this.height = height;

            // 文件流属性描述
            Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO();

            info.fccType               = Win32.mmioFOURCC("vids");
            info.fccHandler            = Win32.mmioFOURCC(codec);
            info.dwScale               = 1;
            info.dwRate                = rate;
            info.dwSuggestedBufferSize = stride * height;

            // 创建流
            if (Win32.AVIFileCreateStream(file, out stream, ref info) != 0)
            {
                throw new ApplicationException("创建流失败!");
            }

            // 压缩选项描述
            Win32.AVICOMPRESSOPTIONS opts = new Win32.AVICOMPRESSOPTIONS();

            opts.fccHandler = Win32.mmioFOURCC(codec);
            opts.dwQuality  = quality;

            //
            // Win32.AVISaveOptions(stream, ref opts, IntPtr.Zero);

            // 创建压缩流
            if (Win32.AVIMakeCompressedStream(out streamCompressed, stream, ref opts, IntPtr.Zero) != 0)
            {
                throw new ApplicationException("创建压缩流失败!");
            }

            // 描述帧格式
            Win32.BITMAPINFOHEADER bih = new Win32.BITMAPINFOHEADER();

            bih.biSize        = Marshal.SizeOf(bih.GetType());
            bih.biWidth       = width;
            bih.biHeight      = height;
            bih.biPlanes      = 1;
            bih.biBitCount    = 24;
            bih.biSizeImage   = 0;
            bih.biCompression = 0;             // BI_RGB

            // 设置帧格式
            if (Win32.AVIStreamSetFormat(streamCompressed, 0, ref bih, Marshal.SizeOf(bih.GetType())) != 0)
            {
                throw new ApplicationException("Failed creating compressed stream");
            }

            // 请求空闲内存
            buf = Marshal.AllocHGlobal(stride * height);

            position = 0;
        }
Ejemplo n.º 2
0
        // Create new AVI file
        public void Open(string fname, int width, int height)
        {
            // close previous file
            Close();

            // calculate stride
            stride = width * 3;
            int r = stride % 4;

            if (r != 0)
            {
                stride += (4 - r);
            }

            // create new file
            if (Win32.AVIFileOpen(out file, fname, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero) != 0)
            {
                throw new ApplicationException("Failed opening file");
            }

            this.width  = width;
            this.height = height;

            // describe new stream
            Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO();

            info.fccType               = Win32.mmioFOURCC("vids");
            info.fccHandler            = Win32.mmioFOURCC(codec);
            info.dwScale               = 1;
            info.dwRate                = rate;
            info.dwSuggestedBufferSize = stride * height;

            // create stream
            if (Win32.AVIFileCreateStream(file, out stream, ref info) != 0)
            {
                throw new ApplicationException("Failed creating stream");
            }

            // describe compression options
            Win32.AVICOMPRESSOPTIONS opts = new Win32.AVICOMPRESSOPTIONS();

            opts.fccHandler = Win32.mmioFOURCC(codec);
            opts.dwQuality  = quality;

            //
            Win32.AVISaveOptions(stream, ref opts, IntPtr.Zero);

            // create compressed stream
            if (Win32.AVIMakeCompressedStream(out streamCompressed, stream, ref opts, IntPtr.Zero) != 0)
            {
                throw new ApplicationException("Failed creating compressed stream");
            }

            // describe frame format
            Win32.BITMAPINFOHEADER bih = new Win32.BITMAPINFOHEADER();

            bih.biSize        = Marshal.SizeOf(bih.GetType());
            bih.biWidth       = width;
            bih.biHeight      = height;
            bih.biPlanes      = 1;
            bih.biBitCount    = 24;
            bih.biSizeImage   = 0;
            bih.biCompression = 0;             // BI_RGB

            // set frame format
            if (Win32.AVIStreamSetFormat(streamCompressed, 0, ref bih, Marshal.SizeOf(bih.GetType())) != 0)
            {
                throw new ApplicationException("Failed creating compressed stream");
            }

            // alloc unmanaged memory for frame
            buf = Marshal.AllocHGlobal(stride * height);

            position = 0;
        }