I am calling
DependencyService to get byte from my image name, sdcard path or URL.
I have working code of bitmap to byte with memory stream compress. But it is taking too much time. and as I need to convert many images my app's performance gets affected.
Here is working code but takes time
// Retrieving the local Resource ID from the name int id = (int)typeof(Resource.Drawable).GetField(fileName).GetValue(null); // Converting Drawable Resource to Bitmap var myImage = BitmapFactory.DecodeResource(Forms.Context.Resources, id); MemoryStream stream = new MemoryStream(); myImage.Compress(Bitmap.CompressFormat.Png, 0, stream); byte bitmapData = stream.ToArray(); return bitmapData;
Here is What I tried
var byteBuffer = ByteBuffer.Allocate(myImage.ByteCount); myImage.CopyPixelsToBuffer(byteBuffer); byteBuffer.Rewind(); var bitmapData = new byte[myImage.ByteCount]; byteBuffer.Get(bitmapData); var bitmapData = byteBuffer.ToArray<byte>();
Above causes an error
can not convert Java.Nio.ByteBuffer to [B
I tried solution from this https://forums.xamarin.com/discussion/comment/60411/#Comment_60411. but it takes the same time
Could anybody help me with this ? Or If anyone has solution to convert directly in XF that would be the best.