Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I wrote the following methods,

C#
public static byte[] BitmapToByteArray(Bitmap image)
        {
            byte[] returns = null;
            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                BitmapData bitmapData = image.LockBits(
                                                new Rectangle(0, 0, image.Width, image.Height),
                                                ImageLockMode.ReadWrite,
                                                image.PixelFormat);
                int noOfPixels = image.Width * image.Height;
                int colorDepth = Bitmap.GetPixelFormatSize(image.PixelFormat);
                int step = colorDepth / 8;
                byte[] bytes = new byte[noOfPixels * step];
                IntPtr address = bitmapData.Scan0;
                Marshal.Copy(address, bytes, 0, bytes.Length);
                ////////////////////////////////////////////////
                ///
                returns = (byte[])bytes.Clone();
                ///
                ////////////////////////////////////////////////
                Marshal.Copy(bytes, 0, address, bytes.Length);
                image.UnlockBits(bitmapData);
            }
            else
            {
                throw new Exception("8bpp indexed image required");
            }
            return returns;
        }


And,

C#
public static Bitmap ByteArrayToBitmap(byte[] bytes, int width, int height, PixelFormat pixelFormat)
        {
            Bitmap bitmap = new Bitmap(width, height, pixelFormat);
            BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            int colorDepth = Bitmap.GetPixelFormatSize(pixelFormat);
            int noOfChannels = colorDepth / 8;
            IntPtr address = bitmapData.Scan0;
            //////////////////////////////////////////////////////////////
            //
            Marshal.Copy(bytes, 0, address, width * height * noOfChannels);
            //
            //////////////////////////////////////////////////////////////
            bitmap.UnlockBits(bitmapData);

            return bitmap;
        }


They seem to be not working,

http://i.stack.imgur.com/e3VFD.png

What has been the problem do you think?

What I have tried:

N.B.


Driver program,

C#
public class MainClass
{
    public static void Main(string [] args)
    {
        Bitmap inputBmp = (Bitmap)Bitmap.FromFile(@"cameraman.gif");

        byte[] bytes = Converter.BitmapToByteArray(inputBmp);//byte[65536]

        Bitmap outputBmp = Converter.ByteArrayToBitmap(bytes, inputBmp.Width, inputBmp.Height, PixelFormat.Format8bppIndexed);

        PictureDisplayForm f = new PictureDisplayForm(inputBmp, outputBmp);
        f.ShowDialog();
    }
}
Posted
Updated 11-Jul-16 11:30am
Comments
Patrice T 11-Jul-16 14:56pm    
Repost ?
[no name] 11-Jul-16 15:00pm    
No.

That was Bitmap to 2D-integer and vice versa.

Code was also different.
dcmuggins 15-Jul-16 9:28am    
I know how to do this and I will post a solution tonight, if you still need one. I am currently at work and don't have access to Visual Studio. Let me know!
[no name] 15-Jul-16 9:54am    
Thank you for your interest.

I solved the problem.

That was actually caused by the ColorPalette.
dcmuggins 15-Jul-16 18:45pm    
Nice work! Thanks for getting back to me

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900