How to convert Bitmap to Byte [] without compression

DimaHaikinDimaHaikin USMember
edited October 2013 in Visual Studio

Hi all.

I'm new to Android and also to C# most of my experience was with C++.

My main Goal is to load a picture, Jpg or BMP, and process it to inverse and save to a new file.

I Managed to Create a Bitmap object containing the data with the following code :

  Bitmap BitMapDat = BitmapFactory.DecodeFile(RealPath, null);

When i attempt to Get it into byte [] array i have an un-handled exception .
Caused by the following code lines :

            byte[] BitmapArray;
            BitmapArray = BitMapDat.ToArray<byte>();

I've Tried an other method which throws me the same exception :

    private byte[] copyToBuffer(Bitmap bitmap)
    {

        ByteBuffer buffer = ByteBuffer.Allocate(bitmap.ByteCount);


        bitmap.CopyPixelsToBuffer(buffer);
        bool check = buffer.HasArray;
        buffer.Rewind();
        if (buffer.HasArray)
        {
            byte [] NewData = buffer.ToArray<byte>();
            return NewData;
        }
        else return null;
    }

My Goal is to have raw data of bitmap including Stride, after processing i should make a new bmp with the result picture.

Project file attached.

Thank you for your help.

Dima

Answers

  • CheesebaronCheesebaron DKInsider, University mod

    What is the exception? And why is this in the Visual Studio forum and not in the Android forum?

  • DimaHaikinDimaHaikin USMember
    edited October 2013

    Hi .
    The exception is the following:

    >     10-28 19:24:48.452 I/MonoDroid(  759): UNHANDLED EXCEPTION: System.InvalidCastException: Unable to cast from 'android/graphics/Bitmap' to '[B'.
    > 10-28 19:24:48.452 I/MonoDroid(  759): at Android.Runtime.JNIEnv.AssertCompatibleArrayTypes (intptr,System.Type) [0x0001a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:592
    > 10-28 19:24:48.452 I/MonoDroid(  759): at Android.Runtime.JNIEnv.GetArray<byte> (intptr) [0x00026] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:1032
    > 10-28 19:24:48.452 I/MonoDroid(  759): at Java.Lang.Object.ToArray<byte> () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:246
    > 10-28 19:24:48.452 I/MonoDroid(  759): at Android.App.Activity.n_OnActivityResult_IILandroid_content_Intent_ (intptr,intptr,int,int,intptr) [0x00014] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1648
    

    Regarding the forum i though it was more relevant in Visual Studio forum.

    Thank you.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Instead of doing this:

    byte[] BitmapArray;
    BitmapArray = BitMapDat.ToArray<byte>();
    

    What happens if you do this?

    var BitmapArray = BitMapDat.ToArray<byte>();

    ?

  • DimaHaikinDimaHaikin USMember

    I get the same exception :(

  • PerHungebergMllerPerHungebergMller DKMember ✭✭✭

    @DimaHaikin a quick search on the inter web gave me this discussion and possible solutions on Stack Owerflow:

    http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c

Sign In or Register to comment.