Forum General

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Convert Bitamp to byte[] without compressing memory stream

trishelwoodtrishelwood USMember ✭✭
edited May 2017 in General

Hello,

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.

Thank you :smile:

Sign In or Register to comment.