0

I have a collection of Bitmaps that are stored in a List<byte[]> object. I want to combine all those Bitmaps into one image (kinda like a paneramic) without converting them back to bitmaps. I've tried using a MemoryStream (see below) but it doesn't seem to work. I always run out of memory or the image comes out corrupted. I would appreciate any thoughts/advice on the matter.

List<byte[]> FrameList = new List<byte[]>();

using (MemoryStream ms = new MemoryStream())
{
    for (int i = 0; i < FrameList.Count; i++)
    {
        //Does not work
        ms.Write(FrameList[i], i * FrameList[i].Length, FrameList[i].Length);
        //Does not work
        ms.Write(FrameList[i], 0, FrameList[i].Length); 
    }
    Bitmap img = (Bitmap)Bitmap.FromStream(ms);
    img.Save("D:\\testing.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
 }
4
  • To combine jpg's you need to decode them, stitch them and encode it back to jpg Commented Sep 9, 2021 at 14:36
  • 2
    Image manipulation doesn't work like that. No structured data manipulation works like that. What you want is not possible without at least some understanding of the data format, so the condition "without converting them back to bitmaps" is not realistically possible. Commented Sep 9, 2021 at 14:38
  • 3
    You can't just add the bytes together and make a large image, there's more to that than just the raw pixel data, like headers and colour details. Commented Sep 9, 2021 at 14:38
  • Appending the bytes of one image to the end of another is like trying to make one of these from one of these by doing this. Commented Sep 9, 2021 at 14:57

1 Answer 1

1

The steps:

  1. calculate the final image size as sum of size of the individual images

  2. create a Bitmap with the final size

  3. get the Graphics object

  4. draw all images using the Graphics object

  5. save the final Bitmap

    var final = new Bitmap(width, height); // calculated final size
    using (var graphics = Graphics.FromImage(final))
    {
        graphics.DrawImage(...); // draw all partial images onto the final bitmap
    } 
    
Sign up to request clarification or add additional context in comments.

1 Comment

The requirement is "without converting them back to bitmaps." and one can't know the width and height with raw bytes.

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.