I'm trying to create a Desktop Recording Application. When I record the full screen, the program works as it is supposed to, but in some cases when I select a specific region from the desktop to record I get an error at:

int result = AVIStreamSetFormat(psCompress, 0, ref bi, (Int32)bi.biSize);
Error in VideoStreamSetFormat: -2147205016

I'm using Xvid MPEG-4 Codec to create AVI video. I think the problem might me that Xvid MPEG-4 Codec does not accept certaint image sizes (width and height). I'm not sure and stuck on this problem and I'm asking if somebody can help me understand why it is not working.

private void SetFormat(IntPtr psCompress)
{
    BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
    bi.biSize = (uint)Marshal.SizeOf(bi);
    bi.biWidth = (Int32)_width;
    bi.biHeight = (Int32)_height;
    bi.biPlanes = 1;
    bi.biBitCount = (short)_frameRate; 
    bi.biCompression = 0;  // 0 = BI_RGB
    bi.biSizeImage = _stride * _height;

    int result = AVIStreamSetFormat(psCompress, 0, ref bi, (Int32)bi.biSize);
    if (result != 0)
    {
        throw new Exception("Error in VideoStreamSetFormat: " + result.ToString());
    }
} 

I found what was the problem. When taking screenshots from selected regions on the desktop I had to be sure that the height and width are divisible to 2. It seems that Xvid MPEG-4 Codec does not accept just any image size.

This question has already been answered. Start a new discussion instead.