Skip to content

Commit 456ff97

Browse files
parhamsaremiknocte
authored andcommitted
GTK: call Pixbuf ctor on MainThread for LoadImageAsync functions
This fix avoid threading problems caused by calling Pixbuf ctor from outside of the MainThread. Upstream PR: xamarin#15503
1 parent 94acebb commit 456ff97

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Xamarin.Forms.Platform.GTK/Renderers/ImageRenderer.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Task<Pixbuf> LoadImageAsync(ImageSource imagesource, CancellationToken cancelati
171171

172172
public sealed class FileImageSourceHandler : IImageSourceHandler
173173
{
174-
public Task<Pixbuf> LoadImageAsync(
174+
public async Task<Pixbuf> LoadImageAsync(
175175
ImageSource imagesource,
176176
CancellationToken cancelationToken = default(CancellationToken),
177177
float scale = 1f)
@@ -188,12 +188,15 @@ public Task<Pixbuf> LoadImageAsync(
188188

189189
if (File.Exists(imagePath))
190190
{
191-
image = new Pixbuf(imagePath);
191+
await Device.InvokeOnMainThreadAsync(() =>
192+
{
193+
image = new Pixbuf(imagePath);
194+
});
192195
}
193196
}
194197
}
195198

196-
return Task.FromResult(image);
199+
return image;
197200
}
198201
}
199202

@@ -210,7 +213,12 @@ public sealed class StreamImagesourceHandler : IImageSourceHandler
210213
.GetStreamAsync(cancelationToken).ConfigureAwait(false))
211214
{
212215
if (streamImage != null)
213-
image = new Pixbuf(streamImage);
216+
{
217+
await Device.InvokeOnMainThreadAsync(() =>
218+
{
219+
image = new Pixbuf(streamImage);
220+
});
221+
}
214222
}
215223

216224
return image;
@@ -238,7 +246,10 @@ public async Task<Pixbuf> LoadImageAsync(
238246
return null;
239247
}
240248

241-
image = new Pixbuf(streamImage);
249+
await Device.InvokeOnMainThreadAsync(() =>
250+
{
251+
image = new Pixbuf(streamImage);
252+
});
242253
}
243254

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

249260
public sealed class FontImageSourceHandler : IImageSourceHandler
250261
{
251-
public Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
262+
public async Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
252263
CancellationToken cancellationToken = new CancellationToken(), float scale = 1)
253264
{
254265
if (!(imageSource is FontImageSource fontImageSource))
255266
return null;
256267

257-
Pixbuf pixbuf;
268+
Pixbuf pixbuf = null;
258269
using (var bmp = new Bitmap((int)fontImageSource.Size, (int)fontImageSource.Size))
259270
{
260271
using (var g = Graphics.FromImage(bmp))
@@ -270,11 +281,15 @@ public Task<Pixbuf> LoadImageAsync(ImageSource imageSource,
270281
using (var stream = new MemoryStream())
271282
{
272283
bmp.Save(stream, ImageFormat.Jpeg);
273-
pixbuf = new Pixbuf(stream.ToArray());
284+
await Device.InvokeOnMainThreadAsync(() =>
285+
{
286+
pixbuf = new Pixbuf(stream.ToArray());
287+
});
288+
274289
}
275290
}
276291

277-
return Task.FromResult(pixbuf);
292+
return pixbuf;
278293
}
279294

280295
static FontFamily GetFontFamily(FontImageSource fontImageSource)

0 commit comments

Comments
 (0)