internal IntPtr ConvertToMemory() { int size = Marshal.SizeOf(typeof(EncoderParameter)); Debug.Assert(size == (16 + 4 + 4 + 4), "wrong size! (" + size + ")"); IntPtr memory = (IntPtr)((long)Marshal.AllocHGlobal(param.Length * size + Marshal.SizeOf(typeof(Int32)))); if (memory == IntPtr.Zero) { throw SafeNativeMethods.StatusException(SafeNativeMethods.OutOfMemory); } Marshal.WriteInt32(memory, param.Length); for (int i = 0; i < param.Length; i++) { Marshal.StructureToPtr(param[i], (IntPtr)((long)memory + Marshal.SizeOf(typeof(Int32)) + i * size), false); } return(memory); }
/// <devdoc> /// Copy the EncoderParameters data into a chunk of memory to be consumed by native GDI+ code. /// /// We need to marshal the EncoderParameters info from/to native GDI+ ourselve since the definition of the managed/unmanaged classes /// are different and the native class is a bit weird. The native EncoderParameters class is defined in GDI+ as follows: /// /// class EncoderParameters { /// UINT Count; // Number of parameters in this structure /// EncoderParameter Parameter[1]; // Parameter values /// }; /// /// We don't have the 'Count' field since the managed array contains it. In order for this structure to work with more than one /// EncoderParameter we need to preallocate memory for the extra n-1 elements, something like this: /// /// EncoderParameters* pEncoderParameters = (EncoderParameters*) malloc(sizeof(EncoderParameters) + (n-1) * sizeof(EncoderParameter)); /// /// Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for /// that (See VSW#451333). /// </devdoc> internal IntPtr ConvertToMemory() { int size = Marshal.SizeOf(typeof(EncoderParameter)); int length = param.Length; IntPtr memory = Marshal.AllocHGlobal(checked (length * size + Marshal.SizeOf(typeof(IntPtr)))); if (memory == IntPtr.Zero) { throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.OutOfMemory); } Marshal.WriteIntPtr(memory, (IntPtr)length); long arrayOffset = checked ((long)memory + Marshal.SizeOf(typeof(IntPtr))); for (int i = 0; i < length; i++) { Marshal.StructureToPtr(param[i], (IntPtr)(arrayOffset + i * size), false); } return(memory); }
public SafeHGlobalHandle(object value) : this(Marshal.SizeOf(value)) { Marshal.StructureToPtr(value, handle, false); }