问题
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