src/Foundation/Media/Gimela.Media.Video.VFW/AVIWriter.cs
/// <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="ApplicationException">Failure of opening video files (the exception message
/// specifies the issues).</exception>
/// 
Create new AVI file and open it for writing.
Create new AVI file and open it for writing .
public void Open(string fileName, int width, int height)
    {
      Close();
      lock (this)
      {
        stride = width * 3;
        if ((stride % 4) != 0)
          stride += (4 - stride % 4);
        if (Win32.AVIFileOpen(out file, fileName, Win32.OpenFileMode.Create | Win32.OpenFileMode.Write, IntPtr.Zero) != 0)
          throw new ApplicationException("Failed opening file");
        this.width = width;
        this.height = height;
        Win32.AVISTREAMINFO info = new Win32.AVISTREAMINFO();
        info.type = Win32.mmioFOURCC("vids");
        info.handler = Win32.mmioFOURCC(codec);
        info.scale = 1;
        info.rate = rate;
        info.suggestedBufferSize = stride * height;
        if (Win32.AVIFileCreateStream(file, out stream, ref info) != 0)
          throw new ApplicationException("Failed creating stream");
        Win32.AVICOMPRESSOPTIONS options = new Win32.AVICOMPRESSOPTIONS();
        options.handler = Win32.mmioFOURCC(codec);
        options.quality = quality;
        if (Win32.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero) != 0)
          throw new ApplicationException("Failed creating compressed stream");
        Win32.BITMAPINFOHEADER bitmapInfoHeader = new Win32.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; 
        if (Win32.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType())) != 0)
          throw new ApplicationException("Failed creating compressed stream");
        buffer = Marshal.AllocHGlobal(stride * height);
        if (buffer == IntPtr.Zero)
          throw new ApplicationException("Insufficient memory for internal buffer");
        position = 0;
      }
    }
public void Open ( string fileName , int width , int height ) { Close ( ) ; lock ( this ) { stride = width * 3 ; if ( ( stride % 4 ) != 0 ) stride += ( 4 - stride % 4 ) ; if ( Win32 . AVIFileOpen ( out file , fileName , 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 . type = Win32 . mmioFOURCC ( " " ) ; info . handler = Win32 . mmioFOURCC ( codec ) ; info . scale = 1 ; info . rate = rate ; info . suggestedBufferSize = stride * height ; if ( Win32 . AVIFileCreateStream ( file , out stream , ref info ) != 0 ) throw new ApplicationException ( " " ) ; Win32 . AVICOMPRESSOPTIONS options = new Win32 . AVICOMPRESSOPTIONS ( ) ; options . handler = Win32 . mmioFOURCC ( codec ) ; options . quality = quality ; if ( Win32 . AVIMakeCompressedStream ( out streamCompressed , stream , ref options , IntPtr . Zero ) != 0 ) throw new ApplicationException ( " " ) ; Win32 . BITMAPINFOHEADER bitmapInfoHeader = new Win32 . 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 ; if ( Win32 . AVIStreamSetFormat ( streamCompressed , 0 , ref bitmapInfoHeader , Marshal . SizeOf ( bitmapInfoHeader . GetType ( ) ) ) != 0 ) throw new ApplicationException ( " " ) ; buffer = Marshal . AllocHGlobal ( stride * height ) ; if ( buffer == IntPtr . Zero ) throw new ApplicationException ( " " ) ; position = 0 ; } }
Create new AVI file and open it for writing.
Create new AVI file and open it for writing .
// close previous file // calculate stride // create new file // describe new stream // create stream // describe compression options // uncomment if video settings dialog is required to show // Win32.AVISaveOptions( stream, ref options ); // create compressed stream // describe frame format // BI_RGB // set frame format // alloc unmanaged memory for frame
Failure of opening video files (the exception message
specifies the issues).
Failure of opening video files ( the exception message specifies the issues ) .
fileName
string
AVI file name to create.
AVI file name to create .
width
int
None
height
int
None
remarks
The method opens (creates) a video files, configure video codec and prepares
the stream for saving video frames with a help of  method.
The method opens ( creates ) a video files configure video codec and prepares the stream for saving video frames with a help of method .