且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Xamarin Forms - 图像到字符串和反向

更新时间:2022-11-11 20:50:29

如果你想要的是一个Image的字符串表示,那么首先你需要从此图像中获取 bytes ,然后将其转换为字符串格式,如 Base64

if what you want is a string representation of a Image then first you need to get the bytes from this image and then convert it into a string format like Base64

但首先我们需要从图像中获取字节,Xamarin.Forms的图像是查看,其中包含来源

But first we need to get the byte from the image, Xamarin.Forms's Image is a View which contains a Source

public class Image : View, IImageController, IElementConfiguration<Image>
{
    public ImageSource Source { get; set; }
}

该来源用于加载将要显示的图像,我们有某种 ImageSource FileImageSource StreamImageSource UriImageSource )但是如果我没弄错,目前没办法在Xamarin.Forms中将ImageSource转换为 bytes ,但我们可以使用本机代码

That source is used to load the image that will be shown, we have a some kinds of ImageSource (FileImageSource, StreamImageSource, UriImageSource) but if I'm not mistaken currently no way to transform ImageSource to bytes in Xamarin.Forms, but we can use native code for such

在android中我们可以使用 IImageSourceHandler 将ImageSource转换为位图并将位图转换为字节

In android we can use IImageSourceHandler to transform an ImageSource to Bitmap and form the Bitmap to bytes

[assembly: Dependency(typeof(ImageLoader))]
public class ImageLoader : IImageLoader
{
    public async Task<byte[]> LoadImageAsync(ImageSource source)
    {
        IImageSourceHandler handler = GetHandlerFor(source);
        var bmp = await handler.LoadImageAsync(source, Forms.Context);
        byte[] result;
        using (Stream ms = new MemoryStream())
        {
            await bmp.CompressAsync(Android.Graphics.Bitmap.CompressFormat.Jpeg, 95, ms);
            result = new byte[ms.Length];
            ms.Position = 0;
            await ms.ReadAsync(result, 0, (int)ms.Length);
        }
        return result;
    }

    private IImageSourceHandler GetHandlerFor(ImageSource source)
    {
        IImageSourceHandler result;
        if (source is FileImageSource) result = new FileImageSourceHandler();
        else if (source is StreamImageSource) result = new StreamImagesourceHandler();
        else result = new ImageLoaderSourceHandler();
        return result;
    }
}



iOS



与android相同我们可以使用IImageSourceHandler转换为UIImage,然后从中获取字节

iOS

Same as android we can use IImageSourceHandler to transform into UIImage and then get the bytes from it

[assembly: Dependency(typeof(ImageLoader))]
public class ImageLoader : IImageLoader
{
    public async Task<byte[]> LoadImageAsync(ImageSource source)
    {
        IImageSourceHandler handler = GetHandlerFor(source);
        UIImage image = await handler.LoadImageAsync(source);
        using (NSData imageData = image.AsPNG())
        {
            return imageData.ToArray();
        }
    }

    private IImageSourceHandler GetHandlerFor(ImageSource source)
    {
        IImageSourceHandler result;
        if (source is FileImageSource) result = new FileImageSourceHandler();
        else if (source is StreamImageSource) result = new StreamImagesourceHandler();
        else result = new ImageLoaderSourceHandler();
        return result;
    }
}



表格



请注意,我插入了 [assembly:Dependecy(typeof(ImageLoader))] ,因此我们可以使用Xamarin Forms来识别并从每个平台带来正确的ImageLoader所以我们像这样使用它

Forms

Note that I inserted [assembly: Dependecy(typeof(ImageLoader))] so we can use the Xamarin Forms to recognize and bring the correct ImageLoader from each Platform so we use it like this

byte[] bytes = await DependencyService.Get<IImageLoader>().LoadImageAsync(imgSource);
string base64String = Convert.ToBase64String(bytes) //convert the binnary to a string representation in base64



note



IImageLoader 是一个简单的界面,如下所示

note

IImageLoaderis a simple interface like the following

public interface IImageLoader
{
    Task<byte[]> LoadImageAsync(ImageSource source);
}