[J#] Convert bitmap to array of bytes

Hi everyone!

 

I'd like to ask you how to convert bitmap or any image to array of bytes. I need that because I want to send this image through the socket (some kind of stream) to my second application - I have client and server. Maybe you know diffrent kind of solution.

Thanks for any ideas.



Answer this question

[J#] Convert bitmap to array of bytes

  • taoo tha mastah

    This is C#, but the concept is the same:

    private byte[] Bitmap2Bytes(Bitmap bmp)

    {

        MemoryStream stream = new MemoryStream();

        bmp.Save(stream, ImageFormat.Bmp);

        return stream.ToArray();

    }

    private Bitmap Bytes2Bitmap(byte[] bytes)

    {

        MemoryStream stream = new MemoryStream(bytes);

        return new Bitmap(stream);

    }

    But if you already have a NetworkStream, you can write directly to that...



  • Bay03

    public ubyte[] Bitmap2Bytes(Bitmap bmp)

    {

    MemoryStream stream = new MemoryStream();

    bmp.Save(stream, ImageFormat.get_Bmp());

    return(stream.ToArray());

    }

    private Bitmap Bytes2Bitmap(ubyte[] bytes)

    {

    MemoryStream stream = new MemoryStream(bytes);

    return new Bitmap(stream);

    }

    Ok. Thanks for help.

     


  • [J#] Convert bitmap to array of bytes