---
title: "Writing AVI Files"
canonical: https://www.graphicsmill.com/docs/gm5/WritingAVIFiles.htm
product: "Graphics Mill 5.5 for .NET"
---

This documentation is for the old version. [Go to the latest Graphics Mill docs](https://www.graphicsmill.com/docs/gm/)

# Writing AVI Files
 
Another nice feature of AVI Processor Add-on for Graphics Mill is a possibility to create new AVI files. Being highly integrated with Graphics Mill for .NET, AVI Processor Add-on can compose video data from instances of [Aurigma.GraphicsMill.Bitmap](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Bitmap.htm.md) class. This way you can use all power of Graphics Mill for .NET imaging features to create and pre-process video frames.
 
## Class Structure
  
The main class you should use to create AVI files with AVI Processor Add-on is [Aurigma.GraphicsMill.Codecs.AviWriter](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_AviWriter.htm.md). Further, this class will be referred as *AVI writer* or merely - *writer*. It can be interpreted as a write-only collection: using the [AddFrame(IFrame)](https://www.graphicsmill.com/docs/gm5/M_Aurigma_GraphicsMill_Codecs_FormatWriter_AddFrame_1_7fae152b.htm.md) method you put objects which implements the [Aurigma.GraphicsMill.Codecs.IFrame](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_IFrame.htm.md) interface (i.e. *frames*).
 
To add an audio data to the writer, an *audio manager* represented by the [Aurigma.GraphicsMill.Codecs.AviAudioManager](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_AviAudioManager.htm.md) class is used. It enables you to add *audio streams* to the video movie. You can read about audio managers in the [Working with Audio Track](https://www.graphicsmill.com/docs/gm5/WorkingwithAudioTrack.htm.md) topic.
 
Also, the writer can use another handy class [Aurigma.GraphicsMill.Codecs.AviWatermark](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_AviWatermark.htm.md) which enables you to put watermarks on frames you add to the writer. You can access it through the [Watermark](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_Watermark.htm.md) property. Read more detailed about watermarks in the [Working with Watermarks](https://www.graphicsmill.com/docs/gm5/WorkingwithWatermarks.htm.md) topic.
 
If you want to put transition effects between some frames, you should set the [AviFrame.Transition](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviFrame_Transition.htm.md) property. Read more detailed about this in the Watermark property. Read more detailed about watermarks in the [Working with Watermarks](https://www.graphicsmill.com/docs/gm5/WorkingwithWatermarks.htm.md) topic.
 
## How to Use AVI Writer
  
The writer object usage can be described with the following steps:
 
1. Create new writer object instance.
2. Initialize its parameters such as compression type, dimensions, frames per second, etc.
3. Open the writer object.
4. Add frames to the writer.
5. If necessary, add audio streams.
6. Close the writer object.

**Important** 
The order of actions listed above is important. E.g. if you open the writer object (using the [Open](https://www.graphicsmill.com/docs/gm5/O_T_Aurigma_GraphicsMill_Codecs_FormatWriter_Open.htm.md) method) before you initialize, say the compression type (using the [CompressorHandler](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_CompressorHandler.htm.md) property), any attempts to change these settings will be disregarded.
 
Also, if you try to add audio data before you add all frames, you may get it truncated (depending on parameters). See the [Working with Audio Track](https://www.graphicsmill.com/docs/gm5/WorkingwithAudioTrack.htm.md) topic for more information about it.
 
Paragraphs below describe how to initialize parameters and add frames more detailed.
 
## Initialize AVI Writer
  
After you create an instance of the writer object, you should specify the parameters which will be used to create an AVI file. Each parameter is configured by an appropriate property of the writer object. In particular:
 
- CompressorHandler property - specifies a code of VfW compressor (FOURCC converted to integer) which should be used to encode the AVI file. An appropriate compressor should be installed on the machine. You can check what compressors are installed as described in the [Working with Compressors](https://www.graphicsmill.com/docs/gm5/WorkingwithCompressors.htm.md) topic.
- [Width](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_Width.htm.md) and [Height](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_Height.htm.md) properties - specify dimensions of the result AVI file. If you omit some of these parameters, it will be taken from the first frame you put to the writer.
- [ResizeFrame](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_ResizeFrame.htm.md) property - specifies whether to resize all frames to values specified by Width and Height properties. If this value is set to `false`, frames are cropped to fit these dimensions.
- [FramesPerSecond](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_FramesPerSecond.htm.md) property - specifies how fast the player should play the AVI file (a number of frames which should be displayed per one second).
- [Quality](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_Quality.htm.md) property - when the compressor implements some lossy algorithm, specifies a quality/size ratio (in range from 0 to 10000).
- [KeyFrameRate](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_KeyFrameRate.htm.md) and [BytesPerSecond](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_BytesPerSecond.htm.md) properties - specify key frame rate and bytes per second parameters for MPEG-based compressors (disregarded by all other compressors).

The code snippet below demonstrate how to initialize the writer:
 
```
Dim writer As New Aurigma.GraphicsMill.Codecs.AviWriter

' Specify dimensions and force all frames to be resized. 
writer.Width = 320
writer.Height = 200
writer.ResizeFrame = True

' Specify MPEG4 by XVID compression. Note, you should make sure that 
' XVID is available on the machine where this code is running. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.XvidMpeg4

' Set MPEG4-related settings:
writer.KeyFrameRate = 160
writer.BytesPerSecond = 128

writer.FramesPerSecond = 24

writer.Open("c:\temp\avioutput\temp.avi")

' ... all other code is skipped for brevity ...
```
 
```csharp
Aurigma.GraphicsMill.Codecs.AviWriter writer = 
    new Aurigma.GraphicsMill.Codecs.AviWriter();

// Specify dimensions and force all frames to be resized. 
writer.Width = 320;
writer.Height = 200;
writer.ResizeFrame = true;

// Specify MPEG4 by XVID Compressor. Note, you should make sure that 
// XVID is available on the machine where this code is running. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.XvidMpeg4;   

// Set MPEG4-related settings:
writer.KeyFrameRate = 160;
writer.BytesPerSecond = 128;

writer.FramesPerSecond = 24;

writer.Open(@"c:\temp\avioutput\temp.avi");

// ... all other code is skipped for brevity ...
```
 
You can have AVI Processor Add-on to open a dialog associated with a compressor so that the user could fill all parameters there. Use [ShowOptionDialog](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_ShowOptionDialog.htm.md) and [OptionsDialogParentHandle](https://www.graphicsmill.com/docs/gm5/P_Aurigma_GraphicsMill_Codecs_AviWriter_OptionsDialogParentHandle.htm.md) properties to do it. If you run this code:
 
```
Dim writer As New Aurigma.GraphicsMill.Codecs.AviWriter

' Settings which you will you set here are used as default 
' values on the dialog
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.MsH263
writer.Quality = 8000

' These two lines make AVI Processor to display the dialog
' If you do not initialize the parent handle, the dialog will be 
' displayed as an independent window (it will be visible in Task Bar).
writer.ShowOptionDialog = True
writer.OptionsDialogParentHandle = Form1.Handle

writer.Open("C:\Temp\writer.avi")

writer.AddFrame(New Aurigma.GraphicsMill.Codecs.AviFrame( _
    New Aurigma.GraphicsMill.Bitmap("c:\animated.gif")))

' ... other frames are skipped for brevity ...

writer.Close()
```
 
```csharp
Aurigma.GraphicsMill.Codecs.AviWriter writer = 
    new Aurigma.GraphicsMill.Codecs.AviWriter();

// Settings which you will set here are used as default 
// values on the dialog. 
writer.CompressorHandler = Aurigma.GraphicsMill.Codecs.AviCompressor.MSVideo1;
writer.Quality = 9000;

// These two lines make AVI Processor to display the dialog
// If you do not initialize the parent handle, the dialog will be 
// displayed as an independent window (it will be visible in Task Bar).
writer.ShowOptionDialog = true;
writer.OptionsDialogParentHandle = form1.Handle;

writer.Open(@"C:\Temp\writer.avi");

writer.AddFrame(new Aurigma.GraphicsMill.Codecs.AviFrame( 
    new Aurigma.GraphicsMill.Bitmap(@"c:\animated.gif")));

// ... other frames are skipped for brevity ...

writer.Close();
```
 
the following dialog will be opened:
 ![Compressor dialog screenshot](https://www.graphicsmill.com/docs/gm5/WritingAviShowDialog.png)
 
## Add Frames
  
As soon as you initialize the writer and call the Open method to get it opened, you can add frames. To do it, you should call the AddFrame(IFrame) method.
 
This method takes an object which implements Aurigma.GraphicsMill.Codecs.IFrame interface. You can get a frame in two ways:
 
- Create new [Aurigma.GraphicsMill.Codecs.AviFrame](https://www.graphicsmill.com/docs/gm5/T_Aurigma_GraphicsMill_Codecs_AviFrame.htm.md) object and put the bitmap (an instance of Aurigma.GraphicsMill.Bitmap class) into it. You can do it in the following way:
 
```
    ' Load the image.jpg file and initialize a frame object with it.
    Dim bitmap As New Aurigma.GraphicsMill.Bitmap("c:\image.jpg")
    Dim frame As New Aurigma.GraphicsMill.Codecs.AviFrame(bitmap)
    
    ' Put this frame to the writer object (it is assumed that the 
    ' writer is properly initialized and opened). 
    writer.AddFrame(frame)
    ```
 
```csharp
    // Load the image.jpg file and initialize a frame object with it.
    Aurigma.GraphicsMill.Bitmap bitmap = 
        new Aurigma.GraphicsMill.Bitmap(@"c:\image.jpg");
    Aurigma.GraphicsMill.Codecs.AviFrame frame = 
        new Aurigma.GraphicsMill.Codecs.AviFrame(bitmap);
    
    // Put this frame to the writer object (it is assumed that the 
    // writer is properly initialized and opened). 
    writer.AddFrame(frame);
    ```
- Get a frame from a reader object. Note, it is not obligatory to use AVI reader. You can use a reader of any other format which is available in Graphics Mill for .NET. For example, you can open some multi-frame format file like animated GIF and convert it to AVI:
 
```
    ' Load the animated.gif file and copy its frames to AVI.
    
    Dim reader As Aurigma.GraphicsMill.Codecs.IFormatReader
    reader = Aurigma.GraphicsMill.Codecs.FormatManager.CreateFormatReader( _
     "c:\animated.gif")
    
    Dim frame As Aurigma.GraphicsMill.Codecs.IFrame
    For Each frame In reader
        ' Put this frame to the writer object (it is assumed that the 
        ' writer is properly initialized and opened). 
        writer.AddFrame(frame)
    Next
    reader.Close()
    ```
 
```csharp
    // Load the animated.gif file and copy its frames to AVI.
    
    Aurigma.GraphicsMill.Codecs.IFormatReader reader;
    reader = Aurigma.GraphicsMill.Codecs.FormatManager.CreateFormatReader(
            @"c:\animated.gif");
    foreach (Aurigma.GraphicsMill.Codecs.IFrame frame in reader)
    {
        // Put this frame to the writer object (it is assumed that the 
        // writer is properly initialized and opened). 
        writer.AddFrame(frame);
    }
    reader.Close();
    ```
 
## See Also
  
### Manual
 
- [Recompressing Existing AVI](https://www.graphicsmill.com/docs/gm5/RecompressingExistingAVI.htm.md)
- [Adding Audio Track](https://www.graphicsmill.com/docs/gm5/AddingAudioTrack.htm.md)
- [Generating Frames](https://www.graphicsmill.com/docs/gm5/GeneratingFrames.htm.md)
- [Putting Logo on AVI File](https://www.graphicsmill.com/docs/gm5/PuttingLogoonAVIFile.htm.md)
- [Creating Transition Effects](https://www.graphicsmill.com/docs/gm5/CreatingTransitionEffects.htm.md)
