Xamarin Android System.Drawing.Image替代方案

时间:2016-04-28 15:19:48

标签: android image xamarin.android streamreader

理想情况下喜欢做

之类的事情
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader(assets.Open("test.png")))
{
    var img = System.Drawing.Image.FromStream(sr.BaseStream);          
}

但Xamarin Android不存在System.Drawing.Image class。 我怎样才能实现上述目标?

1 个答案:

答案 0 :(得分:3)

我找到了另一种方法来克服失踪的System.Drawing.Image

我使用了以下内容:

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

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