Xamarin.Forms菜鸟笔记--9. 图片转byte[](安卓)

有时候需要将sdcard中的图片转换成byte进行处理,针对安卓
以下是代码

调用:

byte[] b1 = GetBytes("/sdcard/screenshot.png");

方法:

private byte[] GetBytes(string filePath)
{
    Bitmap thresholdedBitmap = Bitmap.CreateBitmap(BitmapFactory.DecodeFile(filePath));

    thresholdedBitmap = BitmapFactory.DecodeFile(filePath);

    byte[] bitmapData;
    using (var stream = new MemoryStream())
    {
        thresholdedBitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
        bitmapData = stream.ToArray();
    }
    return bitmapData;
}

猜你喜欢

转载自blog.csdn.net/qq_21703215/article/details/136482854