Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

GTK: Call Pixbuf ctor on MainThread for LoadImageAsync functions #15503

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions Xamarin.Forms.Platform.GTK/Renderers/ImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Task<Pixbuf> LoadImageAsync(ImageSource imagesource, CancellationToken cancelati

public sealed class FileImageSourceHandler : IImageSourceHandler
{
public Task<Pixbuf> LoadImageAsync(
public async Task<Pixbuf> LoadImageAsync(
ImageSource imagesource,
CancellationToken cancelationToken = default(CancellationToken),
float scale = 1f)
Expand All @@ -188,12 +188,15 @@ public Task<Pixbuf> LoadImageAsync(

if (File.Exists(imagePath))
{
image = new Pixbuf(imagePath);
await Device.InvokeOnMainThreadAsync(() =>
{
image = new Pixbuf(imagePath);
});
}
}
}

return Task.FromResult(image);
return image;
}
}

Expand All @@ -210,7 +213,12 @@ public sealed class StreamImagesourceHandler : IImageSourceHandler
.GetStreamAsync(cancelationToken).ConfigureAwait(false))
{
if (streamImage != null)
image = new Pixbuf(streamImage);
{
await Device.InvokeOnMainThreadAsync(() =>
{
image = new Pixbuf(streamImage);
});
}
}

return image;
Expand Down Expand Up @@ -238,7 +246,10 @@ public async Task<Pixbuf> LoadImageAsync(
return null;
}

image = new Pixbuf(streamImage);
await Device.InvokeOnMainThreadAsync(() =>
{
image = new Pixbuf(streamImage);
});
}

return image;
Expand All @@ -248,13 +259,13 @@ public async Task<Pixbuf> LoadImageAsync(

public sealed class FontImageSourceHandler : IImageSourceHandler
{
public Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
public async Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
CancellationToken cancellationToken = new CancellationToken(), float scale = 1)
{
if (!(imageSource is FontImageSource fontImageSource))
return null;

Pixbuf pixbuf;
Pixbuf pixbuf = null;
using (var bmp = new Bitmap((int)fontImageSource.Size, (int)fontImageSource.Size))
{
using (var g = Graphics.FromImage(bmp))
Expand All @@ -270,11 +281,15 @@ public Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
using (var stream = new MemoryStream())
{
bmp.Save(stream, ImageFormat.Jpeg);
pixbuf = new Pixbuf(stream.ToArray());
await Device.InvokeOnMainThreadAsync(() =>
{
pixbuf = new Pixbuf(stream.ToArray());
});

}
}

return Task.FromResult(pixbuf);
return pixbuf;
}

static FontFamily GetFontFamily(FontImageSource fontImageSource)
Expand Down