Click here to Skip to main content
14,427,558 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have two byte[] that represent bitmaps. I want to copy them in to one bitmap, placing them next to each other. Is this possible with marshal? See the code below for how i'd like it to work. Thanks in advance.

//Bitmap 100x100
            byte[] frame1 = null;
            //Bitmap 100x100
            byte[] frame2 = null;

            using (Bitmap bmp = new Bitmap(200, 100))
            {
                BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

                //I want to copy frame1 to location 0,0 in the bitmap
                Marshal.Copy(frame1, 0, bmpData.Scan0, frame1.Length);

                //I want to copy frame2 to location 100,0 in the bitmap
                Marshal.Copy(frame2, 0, bmpData.Scan0, frame2.Length);

                bmp.UnlockBits(bmpData);
            }


What I have tried:

I have two byte[] that represent bitmaps. I want to copy them in to one bitmap, placing them next to each other. Is this possible with marshal? See the code below for how i'd like it to work. Thanks in advance.
Posted
Updated 11-Mar-17 23:31pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

It is not necessary to use Marshal, you can use something like this:
graphics.DrawImage (
			bitmap,
			new Rectangle ( 0, 0, Width, Height ),
			new Rectangle ( 0, 0, Width, Height ),
			GraphicsUnit.Pixel );

As this might be too slow for you, maybe this will be more to your liking: Fast Pointerless Image Processing in .NET[^]
   
v2
Comments
JBHowl 11-Mar-17 10:39am
   
No, that's slow. Graphics is slower to begin with, but i'd also have to convert the byte arrays to bitmaps.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Probably the easiest fast solution for putting the images next to each other would be to use the Bitmap.BitBlt Method[^]. This means that you would have to convert the byte[] arrays to bitmaps first, but using the LockBits method as you do above should make that simple enough. Trying to put the images side-by-side going directly into the final bitmap's data is tricky, as you would need to loop through each row of the result, copying one line of data from each array at a time.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100