Search code examples
c#directshowdirectshow.net

IVMRWindowlessControl.RepaintVideo causing Attempted to divide by zero


I have developed an C# application with Directshow.NET to show camera preview. All is going well almost on every OS but on some of windows OS 10 machines (not every win OS 10, just some of them) preview get failed due to Exception Attempted to divide by zero. at line hr = vmr9Control.RepaintVideo(hControl.Handle, gHDC); inside Paint event of Control.

I searched a lot to find the cause of this exception, even I formattted one of the machine but issue is still there. Also as per observation this exception will not come at the first attempt of preview, after 5-6 iteration of show preview & stop preview causes this exception.

private void SetupGraph(DsDevice dev, Control hControl)
{
...
vmr9Control = (IVMRWindowlessControl)vmr9preview;
vmr9Control.SetVideoClippingWindow(hControl.Handle);
vmr9Control.SetAspectRatioMode(VMRAspectRatioMode.None);
hControl.Paint += new PaintEventHandler(hControl_Paint);
...
}

[System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.LinkDemand, Flags =
System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
void hControl_Paint(object sender, PaintEventArgs e)
{
    if (isPreviewstarted && vmr9Control != null)
    {
        int hr;
        IntPtr gHDC = e.Graphics.GetHdc();
        hr = vmr9Control.RepaintVideo(hControl.Handle, gHDC);
        DsError.ThrowExceptionForHR(hr);
        e.Graphics.ReleaseHdc(gHDC);
    }            
}

Solution

  • I suggest you uninstall and reinstall the graphic card drivers.

    Steps : Uninstall and re-install display drivers.

    To remove the display drivers from Device Manager:

    a. Open Device Manager by clicking the Start button or type Device Manager in the search box.

    b. If you are prompted for an administrator password or confirmation, type the password or provide confirmation.

    c. Look for the Display adapter drivers, right click on it and select Uninstall.

    d. Reboot the computer.

    e. On reboot, Windows 10 should re-install the drivers automatically.