Wgy is ToImageSourceAsync() Extension method not available in .net standard dll?

793
5
10-15-2018 07:41 AM
PeterHoffmann
New Contributor

I want to convert a runtime image into a native image? why cant i do this in a .net standard project?

The according extension method is only available in uwp, android, and ios assemblies...

0 Kudos
5 Replies
dotMorten_esri
Esri Notable Contributor

Because the type returned is platform-specific. All the UI specific types like MapView, SceneView etc are only available at the platform-specific level.

For .NET Standard you use the RuntimeImage type which is cross-platform.

0 Kudos
PeterHoffmann
New Contributor

Ok, then let me pose the question differently.

I have a xamarin.forms project (.net standard). I want to show the thumbnail of an mmpk in a xamarin.forms control. How can i achieve this while I have no ThumbnailUri, just the thumbnail? How do i convert from RuntimeImage to Xamarin.Forms.Image?

Thanks in advance

Peter

0 Kudos
dotMorten_esri
Esri Notable Contributor

I was able to use the StreamImageSource for this:

            RuntimeImage img = await mapView.ExportImageAsync();
            StreamImageSource sis = new StreamImageSource();
            sis.Stream = (token) => img.GetEncodedBufferAsync();
            image.Source = sis;

This is good feedback though. I'll see if it's possible to add this extension method to the Xamarin.Forms library.

0 Kudos
PeterHoffmann
New Contributor

Thx for the confirmation. I came up with the same. It would be great if you'd put it into the xamarin library.

Greetings

Peter

0 Kudos
dotMorten_esri
Esri Notable Contributor

For now, you can put this class in your .NET Standard project, and you'll get the extension method to use everywhere:

using System.Threading.Tasks;
using Esri.ArcGISRuntime.UI;

namespace Esri.ArcGISRuntime.Xamarin.Forms
{
    /// <summary>
    /// Provides conversions of the <see cref="RuntimeImage"/> generic type to a platform specific image type
    /// </summary>
    public static class RuntimeImageExtensions
    {
        /// <summary>
        /// Converts a <see cref="RuntimeImage"/> to a Xamarin.Forms image source.
        /// </summary>
        /// <param name="image">Runtime Image to convert</param>
        /// <returns>A Xamarin.Forms image source</returns>
        public static async Task<global::Xamarin.Forms.ImageSource> ToImageSourceAsync(this RuntimeImage image)
        {
            if (image == null)
                return null;
            var imageStream = await image.GetEncodedBufferAsync();
            return global::Xamarin.Forms.ImageSource.FromStream(() => imageStream);
        }
    }
}
0 Kudos