+2008-11-21 Andreia Gaita <shana@jitted.com>
+
+ * Metafile.cs: Save the underlying stream when creating from a
+ clone under win32 (fixes #438886)
+
2008-10-12 Sebastien Pouliot <sebastien@ximian.com>
* ColorPalette.cs, ImageFormat.cs: Remove unneeded field
nativeObject = ptr;
}
+ // Usually called when cloning images that need to have
+ // not only the handle saved, but also the underlying stream
+ // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
+ internal Metafile (IntPtr ptr, Stream stream)
+ {
+ // under Win32 stream is owned by SD/GDI+ code
+ if (GDIPlus.RunningOnWindows ())
+ this.stream = stream;
+ nativeObject = ptr;
+ }
+
public Metafile (Stream stream)
{
if (stream == null)
nativeObject = ptr;
}
+ // Usually called when cloning images that need to have
+ // not only the handle saved, but also the underlying stream
+ // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
+ internal Bitmap(IntPtr ptr, Stream stream)
+ {
+ // under Win32 stream is owned by SD/GDI+ code
+ if (GDIPlus.RunningOnWindows ())
+ this.stream = stream;
+ nativeObject = ptr;
+ }
+
public Bitmap (int width, int height) : this (width, height, PixelFormat.Format32bppArgb)
{
}
+2008-11-21 Andreia Gaita <shana@jitted.com>
+
+ * Image.cs: On win32, when cloning images initialized from
+ resources, also clone the underlying stream, since it needs to stay
+ around for the life of the image (gdi doesn't clone the underlying
+ stream when cloning images, so when the gc disposes the original, it
+ will also dispose the stream, which invalidates the clone).
+
+ * Bitmap.cs: Save the underlying stream when creating bitmaps from a
+ clone under win32
+ [fixes #438886]
+
2008-11-01 Sebastien Pouliot <sebastien@ximian.com>
* Font.cs: Add comment on .ctor(Font,FontStyle) about the lack of
public object Clone ()
{
+ if (GDIPlus.RunningOnWindows () && stream != null)
+ return CloneFromStream ();
+
IntPtr newimage = IntPtr.Zero;
- Status status = GDIPlus.GdipCloneImage (NativeObject, out newimage);
- GDIPlus.CheckStatus (status);
+ Status status = GDIPlus.GdipCloneImage (NativeObject, out newimage);
+ GDIPlus.CheckStatus (status);
if (this is Bitmap)
return new Bitmap (newimage);
else
return new Metafile (newimage);
}
+
+ // On win32, when cloning images that were originally created from a stream, we need to
+ // clone both the image and the stream to make sure the gc doesn't kill it
+ // (when using MS GDI+ and IStream we must ensure the stream stays alive for all the life of the Image)
+ object CloneFromStream ()
+ {
+ byte[] bytes = new byte [stream.Length];
+ MemoryStream ms = new MemoryStream (bytes);
+ int count = (stream.Length < 4096 ? (int) stream.Length : 4096);
+ byte[] buffer = new byte[count];
+ stream.Position = 0;
+ do {
+ count = stream.Read (buffer, 0, count);
+ ms.Write (buffer, 0, count);
+ } while (count == 4096);
+
+ IntPtr newimage = IntPtr.Zero;
+ newimage = InitFromStream (ms);
+
+ if (this is Bitmap)
+ return new Bitmap (newimage, ms);
+ else
+ return new Metafile (newimage, ms);
+ }
+
}
}