Convert Bitmap into byte[]

DahevosDahevos Dahevos DahevosFRMember
edited February 2013 in Android

Hello,

I have to save a picture into a database (azure). My data model is cross platform, therefore I can't use "Bitmap" type for my model class. That's why I choose to use byte[].

In android, I have a bitmap. I don't success to convert it into a byte[]. How to do it ?

Posts

  • CheesebaronCheesebaron Tomasz Cielecki DKMember, University ✭✭✭✭
    edited February 2013
    byte[] bitmapData;
    using (var stream = new MemoryStream())
    {
        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
        bitmapData = stream.ToArray();
    }
    

    Did you even try to search for an answer? First hit on google when searching for "mono for android get bitmap bytes" gives you this. It is the same approach as on native Android with Java.

  • DahevosDahevos Dahevos Dahevos FRMember

    Hi Chessebaron,

    Thanks for your answer. I already search on the web, and I found that solution.

                            byte[] bigPicBytes = new byte[bigPicCapture.Width * bigPicCapture.Height * 4];
                            MemoryStream stream = new MemoryStream(bigPicBytes);
                            bigPicCapture.Compress(Bitmap.CompressFormat.Png, 80, stream);
                            stream.Flush();
    

    But I had an exception (Compress System.NotSupportedException). Seems your code works, so thanks.

  • MassimilianoFabbriMassimilianoFabbri Massimiliano Fabbri USMember

    Hello Tomasz, I'm using that piece of code and at this line: bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); I got from compiler this error: [mono] Stacktrace: [mono] [mono] at (wrapper delegate-invoke) .invoke_bool__this___intptr_intptr_intptr_JValue[] (intptr,intptr,intptr,Android.Runtime.JValue[]) <0xffffffff> [mono] at Android.Runtime.JNIEnv.CallBooleanMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00053> [mono] at Android.Graphics.Bitmap.Compress (Android.Graphics.Bitmap/CompressFormat,int,System.IO.Stream) <0x001eb> [mono] at Discovery_Service.MainActivity.FileSend () <0x000e3> [mono] at Discovery_Service.MainActivity/c__AnonStorey0.<>m__3 (object,System.EventArgs) <0x000af> [mono] at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) <0x00057> [mono] at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) <0x00063> [mono] at (wrapper dynamic-method) object.2f4f49ef-aea0-4aad-9fe2-b66db563e805 (intptr,intptr,intptr) <0x00043> [mono] at (wrapper native-to-managed) object.2f4f49ef-aea0-4aad-9fe2-b66db563e805 (intptr,intptr,intptr) <0xffffffff> Object reference not set to an instance of an object

    bitmap variable is not null, but the issueseems to be the 'stream' object. I can't see any problem. Do you ? Thanks. Max

  • MassimilianoFabbriMassimilianoFabbri Massimiliano Fabbri USMember
    edited August 2013

    I treid other solutions, but my headhake is exponential.... I got:

    public static int getSizeInBytes(Bitmap bitmap) {
                return bitmap.RowBytes * bitmap.Height;
            }

    `byte[] bitmapData = null; //new byte[1024];

    try {

    ByteBuffer byteBuffer = ByteBuffer.Allocate(getSizeInBytes(_Image)); _Image.CopyPixelsToBuffer(byteBuffer); bitmapData = byteBuffer.ToArray();

    }catch ( Exception err){ Console.WriteLine (err.Message); } ...... ......` but I got always the same error and bitmapData is ever null. [mono] Stacktrace: [mono] [mono] at Android.Runtime.JNIEnv.CallIntMethod (intptr,intptr) <0x00047> [mono] at Android.Graphics.Bitmap.get_RowBytes () <0x0009b> [mono] at Discovery_Service.MainActivity.getSizeInBytes (Android.Graphics.Bitmap) <0x00017> [mono] at Discovery_Service.MainActivity.FileSend () <0x000a7> [mono] at Discovery_Service.MainActivity/c__AnonStorey0.<>m__3 (object,System.EventArgs) <0x000af> [mono] at Android.Views.View/IOnClickListenerImplementor.OnClick (Android.Views.View) <0x00057> [mono] at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) <0x00063> [mono] at (wrapper dynamic-method) object.00ee544d-1c90-4c5e-96a0-f917fb0ddeea (intptr,intptr,intptr) <0x00043> [mono] at (wrapper native-to-managed) object.00ee544d-1c90-4c5e-96a0-f917fb0ddeea (intptr,intptr,intptr) <0xffffffff> Object reference not set to an instance of an object

    ps: code formatting of this forum (and the whole forum system too) sucks!!

  • EvilGeniusJamieEvilGeniusJamie Jamie Duggan GBMember

    @Dahevos:

    The problem you were having is that you were generating the array based on the size of the picture in pixels, based on 4 bytes per pixel - when compressed, the array won't be that large. Using the ToArray function of MemoryStream creates the array only as long as it needs.

    Not 100% but I'm sure there's an issue with creating the memory stream and passing an array to its instantiate function, as it's basically converting this array to a memory stream, then you're trying to over-write the array in the Compress function...

  • PaulBettsPaulBetts Paul Betts USMember, Beta

    If you want to process images in a cross-platform way, check out https://github.com/paulcbetts/splat

Sign In or Register to comment.