3

I have an array of Bitmaps that I'm trying to convert into an array of bytes where each index of the byte array represents a Bitmap. I'm having some trouble figuring out how to do this. If anyone has a suggestion, let me know. Thanks!

private void ConvertBitmapToBytes(Bitmap[] BitmapArray)
{
    byte[][] BitmapBytes = new byte[BitmapArray.Length][];
    ImageConverter convert = new ImageConverter();
    for (int i = 0; i < BitmapArray.Length; i++)
    {
        BitmapBytes[i] = new byte[BitmapArray.Length];
        BitmapBytes[i][i] = convert.ConvertTo(BitmapArray[i], typeof(byte[]));
    }
}

1 Answer 1

5

Try this:

public byte[] ImageToByte(Bitmap image){
    using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, ImageFormat.Bmp);
        byte[] imageBytes = ms.ToArray();
        return imageBytes;
    }
}

And then, your code will become this:

private void ConvertBitmapToBytes(Bitmap[] BitmapArray)
{
    byte[][] BitmapBytes = new byte[BitmapArray.Length][];
    for (int i = 0; i < BitmapArray.Length; i++)
    {
        BitmapBytes[i] = ImageToByte(BitmapArray[i]);
    }
}

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this is exactly what I'm looking for. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.