System.ArgumentNullException in SharpGL.WPF.OpenGLControl.timer_Tick

Apr 22, 2013 at 10:42 AM
Hi, Thanks U SharpGL, It saves my life when porting my project to WPF:)

I have a problem below, Thanks for any one could help me to figure out the reason.
(1) I create a WPF project and imported the SharpGL dll(version 2.1)
(2) in xaml wrote the UI code below:
            <Grid Grid.Row="1" Margin="0,0,0,1">
                    <ImageBrush ImageSource="/A.View.Touch;component/Images/Common/picture_bg.png" />
                <Border Name="frontBorder" Margin="8" BorderThickness="3" BorderBrush="{Binding LeftBoxColor}">
                           Name="front3DImg" Tag="0"


                        <Canvas x:Name="leftCanvas" MouseLeftButtonDown="LeftCanvas_MouseDown" MouseRightButtonDown="LeftCanvas_MouseRightButtonDown" MouseMove="LeftCanvas_MouseMove" MouseLeftButtonUp="LeftCanvas_MouseUp" MouseLeave="LeftCanvas_MouseLeave" Background="Transparent" />
                <Image Name="frontOtherImg" HorizontalAlignment="Right" Width="50" Height="50"  Stretch="Fill" Margin="0,8,8,0" VerticalAlignment="Top" Visibility="Hidden" Source="/A.View.Touch;component/Images/Main/other.png" />
(3) when I run this app, exception beow will occur.
(when this exception occured, this window is still hide by another login window)

value can not be null
parameter name: bitmap

Stack Trace:
在 System.Windows.Interop.Imaging.CriticalCreateBitmapSourceFromHBitmap(IntPtr bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions, WICBitmapAlphaChannelOption alphaOptions)
在 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(IntPtr bitmap, IntPtr palette, Int32Rect sourceRect, BitmapSizeOptions sizeOptions)
在 SharpGL.WPF.BitmapConversion.HBitmapToBitmapSource(IntPtr hBitmap)
在 SharpGL.WPF.OpenGLControl.timer_Tick(Object sender, EventArgs e)
在 System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
Apr 22, 2013 at 10:44 AM
Let me add some additional information please.

In the same window, there is also another OpenGLControl,
that's to say: there are two OpenGLControl in the same WPF window.
Apr 24, 2013 at 10:58 AM
Can you try changing the render context type to DIB section temporarily to see if that helps? If so, it might be in the FBO implementation
May 2, 2013 at 11:32 AM
Thanks for Ur replay.

I removed the line below from the xaml code of the first OpenGLControl, It's OK now.

(The other OpenGLControl still has the line below in it's xaml defination:
May 2, 2013 at 2:00 PM
Add some additional information.

My OS is Windows 8.
May 3, 2013 at 7:17 AM
Once you changed from FBO to DIB Section, did that fix the issue?
May 3, 2013 at 7:39 AM
Sorry for the delay to test.

I did the two steps below, and the display is also OK.
(1) Added RenderContextType="DIBSection" into the first OpenGLControl;
(2) Changed the RenderContextType from "FBO" to "DIBSection" in the second OpenGLControl.

Maybe the problem is in FBO render type.
May 3, 2013 at 7:46 AM
It's also OK when I totally removed the line of RenderContextType= from both OpenGLControl.

It looks like that the default value for RenderContextType is "DIBSection".
Sep 2, 2013 at 8:45 PM
Same problem for me on Windows 8. It works fine in a form with just a Grid, but when I add other stuff in the grid, I get the same error. Switching to DIB or leaving the RenderContextType attribute out solves part of the problem. I get an other "old friend" in return in my custom SharpGL based code: Extension function glGenBuffers not supported. These extension method exceptions truly give me a headache. Anyone knows more on this topic?
Sep 3, 2013 at 7:04 AM
Would using the invisible window option provide a solution in combination with a windows forms host, containing a control to supply a handle for the device context?
Sep 3, 2013 at 7:32 AM
For anyone having the same problems, I might have the solution:

I didn't see the canvas and I don't know, why the OpenGL control is not nested inside it, but it works for me! After encountering the issue, I stumbled upon a problem someone else had with TAO and solved it by starting from scratch. First with the Windows Forms Host solution and finally with a simple Canvas for providing the device context... if I'm right, but I must admit, I skipped the article and tried out the Canvas immiately. :-)

Here's the link to the article: “{Binding OpenGl To WPF}” : Part 2

Anyway, embedding the OpenGLControl in a Canvas was the solution to my problem and kept me from taking aspirin :-D