Reputation: 7
I need to convert bitmap to Imagesource
. I have searched online but I can only find examples the other way around.
Do you please have any examples?
Upvotes: 0
Views: 2235
Reputation: 1013
how to convert Bitmap to Imagesource Xamarin
You coudl convert the bitmap to stream first on the native platform, then get the imageSource from the stream. You could use DependencyService to achieve the function and call the method in the shared project.
Check the code:
Create an interface in the shared project.
public interface IGetFileStream
{
MemoryStream getStream();
}
Implement the interface in the required platform projects.
[assembly: Dependency(typeof(DroidGetStreamImplement))]
namespace App19F_9.Droid
{
public class DroidGetStreamImplement : IGetFileStream
{
public MemoryStream getStream()
{
var bitmap = ...;
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
bitmap.Recycle();
return stream;
}
}
}
Resolve the platform implementations from shared code.
public partial class Page5 : ContentPage
{
public Page5()
{
InitializeComponent();
var stream = DependencyService.Get<IGetFileStream>().getStream();
image.Source = ImageSource.FromStream(stream);
}
}
Upvotes: 2