Reputation:
Ideally would like to do something like
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("test.png")))
{
var img = System.Drawing.Image.FromStream(sr.BaseStream);
}
but System.Drawing.Image class
does not exist with Xamarin Android.
How can I achieve the above?
Upvotes: 2
Views: 1643
Reputation:
I have found an alternative to overcome the missing System.Drawing.Image
.
I have used the following:
using (StreamReader sr = new StreamReader(assets.Open("test.png")))
{
BinaryReader binreader = new BinaryReader(sr.BaseStream);
var allData = ReadAllBytes(binreader);
Bitmap bitmap = BitmapFactory.DecodeByteArray(allData, 0, allData.Length);
imageView.SetImageBitmap(bitmap);
}
and
public static byte[] ReadAllBytes(BinaryReader reader)
{
const int bufferSize = 4096;
using (var ms = new MemoryStream())
{
byte[] buffer = new byte[bufferSize];
int count;
while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
ms.Write(buffer, 0, count);
return ms.ToArray();
}
}
Upvotes: 3