JBryanB
JBryanB

Reputation: 33

Converting An Array of Byte[] To A Bitmap

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);
 }

Upvotes: 0

Views: 279

Answers (1)

Martin Staufcik
Martin Staufcik

Reputation: 9502

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
    } 
    

Upvotes: 1

Related Questions