[Mono-devel-list] Problem with parameter definition of GdipImageCodecInfo::MarshalTo

Stefan Domnanovits domo at domnanovits.at
Thu May 27 18:08:39 EDT 2004

When calling a few lines of code like

void foo()
   Bitmap bmp = new Bitmap (42, 42);
   Graphics g = Graphics.FromImage(bmp);
   g.Clear (Color.Blue);
   bmp.Save ("myimage.jpg", ImageFormat.Jpeg); // exception thrown here

an NullReferenceException was thrown. (current CVS Version on Mac OS X).

I did a little testing and discovered that the error occurred in the
System.Drawing.dll in the call to the static MarshalTo() method of
the GdipImageCodecInfo struct. I changed the definition of the method from

   internal static void MarshalTo (GdipImageCodecInfo gdipcodec,
ImageCodecInfo codec)


   internal static void MarshalTo (ref GdipImageCodecInfo gdipcodec,
ImageCodecInfo codec)

providing the first parameter as "ref", which fixed the error.

The methods maps parameters of the unmanaged gdiplus structure to a
managed class.
I'm a little confused now, as imho this should not have made any
difference. A structure is a value type after all, which means the
runtime should have created a complete copy of the structure for the
GdipImageCodecInfo.MarshalTo() method. Is there something different with 
structures, allocated by a Marshal.PtrToStructure() call?

kind regards

Stefan Domnanovits

my current system:
Mac OS X 10.3.4
latest CVS Version of Mono 2004-05-27
latest CVS Version of libgdiplus 2004-05-27

More information about the Mono-devel-list mailing list