diff --git a/binding/Binding/SKImage.cs b/binding/Binding/SKImage.cs index cbf32fc9fc..262ba9831e 100644 --- a/binding/Binding/SKImage.cs +++ b/binding/Binding/SKImage.cs @@ -569,7 +569,9 @@ public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX, int srcY, SKImageCachingHint cachingHint) { var cinfo = SKImageInfoNative.FromManaged (ref dstInfo); - return SkiaApi.sk_image_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY, cachingHint); + var result = SkiaApi.sk_image_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY, cachingHint); + GC.KeepAlive (this); + return result; } public bool ReadPixels (SKPixmap pixmap) => @@ -583,7 +585,9 @@ public bool ReadPixels (SKPixmap pixmap, int srcX, int srcY, SKImageCachingHint if (pixmap == null) throw new ArgumentNullException (nameof (pixmap)); - return SkiaApi.sk_image_read_pixels_into_pixmap (Handle, pixmap.Handle, srcX, srcY, cachingHint); + var result = SkiaApi.sk_image_read_pixels_into_pixmap (Handle, pixmap.Handle, srcX, srcY, cachingHint); + GC.KeepAlive (this); + return result; } // ScalePixels diff --git a/binding/Binding/SKSurface.cs b/binding/Binding/SKSurface.cs index 9065a3452d..3adc84f005 100644 --- a/binding/Binding/SKSurface.cs +++ b/binding/Binding/SKSurface.cs @@ -346,7 +346,9 @@ public bool PeekPixels (SKPixmap pixmap) public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX, int srcY) { var cinfo = SKImageInfoNative.FromManaged (ref dstInfo); - return SkiaApi.sk_surface_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY); + var result = SkiaApi.sk_surface_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY); + GC.KeepAlive (this); + return result; } internal static SKSurface GetObject (IntPtr handle) => diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Android/AndroidExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Android/AndroidExtensions.cs index 4299f9ee38..6fba1f1bde 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Android/AndroidExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Android/AndroidExtensions.cs @@ -1,4 +1,5 @@ -using Android.Graphics; +using System; +using Android.Graphics; namespace SkiaSharp.Views.Android { @@ -130,7 +131,9 @@ public static Bitmap ToBitmap(this SKBitmap skiaBitmap) { using (var pixmap = skiaBitmap.PeekPixels()) { - return pixmap.ToBitmap(); + var bmp = pixmap.ToBitmap(); + GC.KeepAlive(skiaBitmap); + return bmp; } } @@ -183,7 +186,9 @@ public static Bitmap ToBitmap(this SKImage skiaImage) { using (var pixmap = skiaImage.PeekPixels()) { - return pixmap.ToBitmap(); + var bmp = pixmap.ToBitmap(); + GC.KeepAlive(skiaImage); + return bmp; } } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs index 40930594c0..0aaa28c22d 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Apple/AppleExtensions.cs @@ -176,6 +176,7 @@ public static CGImage ToCGImage(this SKBitmap skiaBitmap) provider, null, false, CGColorRenderingIntent.Default); } + GC.KeepAlive(skiaBitmap); return cgImage; } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Gtk/GTKExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Gtk/GTKExtensions.cs index 72800bb8e9..8423a1906c 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Gtk/GTKExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Gtk/GTKExtensions.cs @@ -1,5 +1,7 @@ using Gdk; +using GC = System.GC; + namespace SkiaSharp.Views.Gtk { public static class GTKExtensions @@ -91,9 +93,12 @@ public static Pixbuf ToPixbuf(this SKImage skiaImage) public static Pixbuf ToPixbuf(this SKBitmap skiaBitmap) { - using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels())) + using (var pixmap = skiaBitmap.PeekPixels()) + using (var image = SKImage.FromPixels(pixmap)) { - return image.ToPixbuf(); + var pixbuf = image.ToPixbuf(); + GC.KeepAlive(skiaBitmap); + return pixbuf; } } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.Shared/Extensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.Shared/Extensions.cs index 35467012a9..6961f4ac6e 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.Shared/Extensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.Shared/Extensions.cs @@ -137,9 +137,12 @@ public static System.Drawing.Bitmap ToBitmap(this SKImage skiaImage) public static System.Drawing.Bitmap ToBitmap(this SKBitmap skiaBitmap) { - using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels())) + using (var pixmap = skiaBitmap.PeekPixels()) + using (var image = SKImage.FromPixels(pixmap)) { - return image.ToBitmap(); + var bmp = image.ToBitmap(); + GC.KeepAlive(skiaBitmap); + return bmp; } } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.UWP/UWPExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.UWP/UWPExtensions.cs index 958c7ff16e..6118594ed6 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.UWP/UWPExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.UWP/UWPExtensions.cs @@ -96,9 +96,12 @@ public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage) public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap) { - using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels())) + using (var pixmap = skiaBitmap.PeekPixels()) + using (var image = SKImage.FromPixels(pixmap)) { - return image.ToWriteableBitmap(); + var wb = image.ToWriteableBitmap(); + GC.KeepAlive(skiaBitmap); + return wb; } } diff --git a/source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs b/source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs index 92284d51b7..d347628896 100644 --- a/source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs +++ b/source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs @@ -1,4 +1,5 @@ -using System.Windows; +using System; +using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -85,9 +86,12 @@ public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage) public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap) { - using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels())) + using (var pixmap = skiaBitmap.PeekPixels()) + using (var image = SKImage.FromPixels(pixmap)) { - return image.ToWriteableBitmap(); + var wb = image.ToWriteableBitmap(); + GC.KeepAlive(skiaBitmap); + return wb; } }