Convert UWP WriteableBitmap to XamarinForms Image

心不动则不痛 提交于 2020-01-25 23:45:33

问题


is there a way to convert a UWP WriteableBitmap to a XamarinForms Image? Trying a simple convert ends up with "cannot implicitly convert type WriteableBitmap to Xamarin Forms Image".

Do I have to convert it before to a base64 string? Or how would you handle that?

thanks


回答1:


You could find FromStream method in the Xamarin.Forms.ImageSource. For your requirement, you could get stream of WriteableBitmap form uwp project via DependencyService. And converter it to ImageSource in the PCL.

Example:

Interface

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

implement

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);
    }
 });
}

Usage

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


来源:https://stackoverflow.com/questions/46882657/convert-uwp-writeablebitmap-to-xamarinforms-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!