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

152
5
10-15-2018 07:41 AM
Highlighted
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...

Reply
0 Kudos
5 Replies
Highlighted
Esri Frequent 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.

Reply
0 Kudos
Highlighted
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

Reply
0 Kudos
Highlighted
Esri Frequent 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.

Reply
0 Kudos
Highlighted
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

Reply
0 Kudos
Highlighted
Esri Frequent 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);
        }
    }
}
Reply
0 Kudos