Esempio n. 1
0
        /// <summary>
        /// Version of <see cref="AVISaveOptions(IntPtr, int, int, IntPtr[], IntPtr[])"/> for one stream only.
        /// </summary>
        ///
        /// <param name="stream">Stream to configure.</param>
        /// <param name="options">Stream options.</param>
        ///
        /// <returns>Returns TRUE if the user pressed OK, FALSE for CANCEL, or an error otherwise.</returns>
        public static int AVISaveOptions(IntPtr stream, ref AVICOMPRESSOPTIONS options)
        {
            IntPtr[] streams = new IntPtr[1];
            IntPtr[] infPtrs = new IntPtr[1];

            // alloc unmanaged memory
            IntPtr mem = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AVICOMPRESSOPTIONS)));

            // copy from managed structure to unmanaged memory
            Marshal.StructureToPtr(options, mem, false);

            streams[0] = stream;
            infPtrs[0] = mem;

            // show dialog with a list of available compresors and configuration
            int ret = AVISaveOptions(IntPtr.Zero, 0, 1, streams, infPtrs);

            // copy from unmanaged memory to managed structure
            options = (AVICOMPRESSOPTIONS)Marshal.PtrToStructure(mem, typeof(AVICOMPRESSOPTIONS));

            // free AVI compression options
            AVISaveOptionsFree(1, infPtrs);

            // clear it, because the information already freed by AVISaveOptionsFree
            options.format = 0;
            options.parameters = 0;

            // free unmanaged memory
            Marshal.FreeHGlobal(mem);

            return ret;
        }
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        ///
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        ///
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        ///
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while creating new video file. See exception message.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory for internal buffer.</exception>
        /// <exception cref="ArgumentException">Video file resolution must be a multiple of two.</exception>
        public void Open(string fileName, int width, int height, bool showOptions = false)
        {
            // close previous file
            Close();

            // check width and height
            if (((width & 1) != 0) || ((height & 1) != 0))
            {
                throw new ArgumentException("Video file resolution must be a multiple of two.");
            }

            bool success = false;

            try
            {
                lock (sync)
                {
                    // calculate stride
                    stride = width * 3;
                    if ((stride % 4) != 0)
                        stride += (4 - stride % 4);

                    // create new file
                    if (NativeMethods.AVIFileOpen(out file, fileName, OpenFileMode.Create | OpenFileMode.Write, IntPtr.Zero) != 0)
                        throw new IOException("Failed opening the specified file.");

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

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

                    info.type = NativeMethods.mmioFOURCC("vids");
                    info.handler = NativeMethods.mmioFOURCC(codec);
                    info.scale = 1;
                    info.rate = rate;
                    info.suggestedBufferSize = stride * height;

                    // create stream
                    if (NativeMethods.AVIFileCreateStream(file, out stream, ref info) != 0)
                        throw new Exception("Failed creating stream.");

                    // describe compression options
                    AVICOMPRESSOPTIONS options = new AVICOMPRESSOPTIONS();

                    options.handler = NativeMethods.mmioFOURCC(codec);
                    options.quality = quality;

                    if (showOptions)
                    {
                        NativeMethods.AVISaveOptions(stream, ref options);
                    }

                    // create compressed stream
                    if (NativeMethods.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero) != 0)
                        throw new Exception("Failed creating compressed stream.");

                    // describe frame format
                    BITMAPINFOHEADER bitmapInfoHeader = new BITMAPINFOHEADER();

                    bitmapInfoHeader.size = Marshal.SizeOf(bitmapInfoHeader.GetType());
                    bitmapInfoHeader.width = width;
                    bitmapInfoHeader.height = height;
                    bitmapInfoHeader.planes = 1;
                    bitmapInfoHeader.bitCount = 24;
                    bitmapInfoHeader.sizeImage = 0;
                    bitmapInfoHeader.compression = 0; // BI_RGB

                    // set frame format
                    if (NativeMethods.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())) != 0)
                        throw new Exception("Failed setting format of the compressed stream.");

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

                    if (buffer == IntPtr.Zero)
                    {
                        throw new OutOfMemoryException("Insufficient memory for internal buffer.");
                    }

                    position = 0;
                    success = true;
                }
            }
            finally
            {
                if (!success)
                {
                    Close();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        ///
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        ///
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        ///
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while creating new video file. See exception message.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory for internal buffer.</exception>
        /// <exception cref="ArgumentException">Video file resolution must be a multiple of two.</exception>
        public void Open()
        {
            // close previous file
            Close();

            this.width = Options.CaptureArea.Width;
            this.height = Options.CaptureArea.Height;

            // check width and height
            if (((width & 1) != 0) || ((height & 1) != 0))
            {
                throw new ArgumentException("Video file resolution must be a multiple of two.");
            }

            bool success = false;

            try
            {
                lock (sync)
                {
                    // calculate stride
                    stride = width * 3;
                    if ((stride % 4) != 0)
                        stride += (4 - stride % 4);

                    // create new file
                    if (NativeMethods.AVIFileOpen(out file, Options.OutputPath, OpenFileMode.Create | OpenFileMode.Write, IntPtr.Zero) != 0)
                        throw new IOException("Failed opening the specified file.");

                    this.rate = Options.ScreenRecordFPS;

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

                    info.type = NativeMethods.mmioFOURCC("vids");
                    info.handler = NativeMethods.mmioFOURCC("DIB ");
                    info.scale = 1;
                    info.rate = rate;
                    info.suggestedBufferSize = stride * height;

                    // create stream
                    if (NativeMethods.AVIFileCreateStream(file, out stream, ref info) != 0)
                        throw new Exception("Failed creating stream.");

                    if (Options.AVI.CompressOptions.handler == 0)
                    {
                        // describe compression options
                        Options.AVI.CompressOptions.handler = NativeMethods.mmioFOURCC("DIB ");
                    }

                    if (Options.ShowAVIOptionsDialog)
                    {
                        AVICOMPRESSOPTIONS options = new AVICOMPRESSOPTIONS();
                        options.handler = Options.AVI.CompressOptions.handler;
                        options.quality = Options.AVI.CompressOptions.quality;
                        options.flags = 8; // AVICOMPRESSF_VALID
                        int result = NativeMethods.AVISaveOptions(stream, ref options, Options.ParentWindow);
                        if (result == 1)
                        {
                            Options.AVI.CompressOptions = options;
                        }
                    }

                    // create compressed stream
                    if (NativeMethods.AVIMakeCompressedStream(out streamCompressed, stream, ref Options.AVI.CompressOptions, IntPtr.Zero) != 0)
                        throw new Exception("Failed creating compressed stream.");

                    // describe frame format
                    BITMAPINFOHEADER bitmapInfoHeader = new BITMAPINFOHEADER(width, height, 24);

                    // set frame format
                    if (NativeMethods.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())) != 0)
                        throw new Exception("Failed setting format of the compressed stream.");

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

                    if (buffer == IntPtr.Zero)
                    {
                        throw new OutOfMemoryException("Insufficient memory for internal buffer.");
                    }

                    position = 0;
                    success = true;
                }
            }
            finally
            {
                if (!success)
                {
                    Close();
                }
            }
        }
Esempio n. 4
0
 public static extern int AVIMakeCompressedStream(out IntPtr compressedStream, IntPtr sourceStream, ref AVICOMPRESSOPTIONS options, IntPtr clsidHandler);
Esempio n. 5
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        ///
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        ///
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        ///
        /// <exception cref="System.IO.IOException">Failed opening the specified file.</exception>
        /// <exception cref="VideoException">A error occurred while creating new video file. See exception message.</exception>
        /// <exception cref="OutOfMemoryException">Insufficient memory for internal buffer.</exception>
        /// <exception cref="ArgumentException">Video file resolution must be a multiple of two.</exception>
        public void Open(string fileName, int width, int height, bool showOptions = false)
        {
            // close previous file
            Close();

            // check width and height
            if (((width & 1) != 0) || ((height & 1) != 0))
            {
                throw new ArgumentException("Video file resolution must be a multiple of two.");
            }

            bool success = false;

            try
            {
                lock (sync)
                {
                    // calculate stride
                    stride = width * 3;
                    if ((stride % 4) != 0)
                    {
                        stride += (4 - stride % 4);
                    }

                    // create new file
                    if (NativeMethods.AVIFileOpen(out file, fileName, OpenFileMode.Create | OpenFileMode.Write, IntPtr.Zero) != 0)
                    {
                        throw new IOException("Failed opening the specified file.");
                    }

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

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

                    info.type                = NativeMethods.mmioFOURCC("vids");
                    info.handler             = NativeMethods.mmioFOURCC(codec);
                    info.scale               = 1;
                    info.rate                = rate;
                    info.suggestedBufferSize = stride * height;

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

                    // describe compression options
                    AVICOMPRESSOPTIONS options = new AVICOMPRESSOPTIONS();

                    options.handler = NativeMethods.mmioFOURCC(codec);
                    options.quality = quality;

                    if (showOptions)
                    {
                        NativeMethods.AVISaveOptions(stream, ref options);
                    }

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

                    // describe frame format
                    BITMAPINFOHEADER bitmapInfoHeader = new BITMAPINFOHEADER(width, height, 24);

                    // set frame format
                    if (NativeMethods.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())) != 0)
                    {
                        throw new Exception("Failed setting format of the compressed stream.");
                    }

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

                    if (buffer == IntPtr.Zero)
                    {
                        throw new OutOfMemoryException("Insufficient memory for internal buffer.");
                    }

                    position = 0;
                    success  = true;
                }
            }
            finally
            {
                if (!success)
                {
                    Close();
                }
            }
        }