将UWP WriteableBitmap转换为XamarinForms图像

时间:2017-10-23 05:45:39

标签: image xamarin.forms uwp

有没有办法将UWP WriteableBitmap转换为XamarinForms图像?尝试简单的转换最终会“无法将类型WriteableBitmap隐式转换为Xamarin Forms Image”。

我必须先将其转换为base64字符串吗?或者你会如何处理?

感谢

1 个答案:

答案 0 :(得分:0)

您可以在Xamarin.Forms.ImageSource中找到FromStream方法。根据您的要求,您可以通过DependencyService获取WriteableBitmap表单uwp项目的流。并将其转换为PCL中的ImageSource

示例:

<强>接口

public interface IStreamToImage
{
    Task GetStreamFormUWP(Action<Stream> StreamBlock);
}

<强>实施

public async Task GetStreamFormUWP(Action<Stream> StreamBlock)
{ 
    await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => {

       .......

       using (Stream stream = MyBitmap.PixelBuffer.AsStream()){
       await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length);
       StreamBlock(stream);
    }
 });
}

<强>用法

await DependencyService.Get<IStreamToImage>().GetStreamFormUWP( (stream) =>
{          
    image.Source = ImageSource.FromStream(() => stream);
});