IGraphicsWindow.DIB

 
 
 
Posted by:ofer_z
Data created:18 August 2010

Hi Marsel,

How can I access IGraphicsWindow.DIB? When I try to access it directly I get an "An object reference is required for the non-static field, method, or property 'Autodesk.Max.IGraphicsWindow.DIB.get'" error.

 

Thanks,

o

Hey again,

OK, this is where I got so far:

public static Bitmap GrabViewport()
        {
            IViewExp activeViewport = global.COREInterface.ActiveViewport;
            IGraphicsWindow gw = activeViewport.Gw;
            return gw.DIB;
        }

 

But when I call this function through maxscript (that's my easiest way to test it for now) I get the following error:

-- Runtime error: dotNet runtime exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Any idea what I'm doing wrong?

 

Thanks,

o

Never mind, I think I got it. This seems to work:

public static Bitmap GrabViewport()
        {
            IViewExp activeViewport = global.COREInterface.ActiveViewport;
            IGraphicsWindow gw = activeViewport.Gw;
            Bitmap bm = gw.DIB;
            return bm;
        }

 

But I don't understand why I have to cast gw.DIB into a new variable. Why can't I directly return gw.DIB?

 

Thanks,

o

No really sure why it would work in second example and not in the first. One thing you have to be careful when dealing with calls like this is to make sure they happen on the main 3dsmax thread. If you try to invoke the DIB property from another thread, for example, you can get errors/crashes. That is why ISynchronizeInvoke is passed into plugin along with IGlobal.

Other than that I use gw.DIB all the time, but I do catch access violation exceptions which occur rarely (and are caused by some invalid 3dsmax setups):

try
{
bitmap = viewportContext.GraphicsWindow.DIB;
}
catch( System.AccessViolationException )
{
allowResizeViewport = false;
Plugin.Log( Plugin.LogType.Warning, "Error retrieving bitmap" );
}

Marsel Khadiyev (Software Developer, EPHERE Inc.)