有时候需要将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;
}