简体   繁体   English

如何在C#中将位图转换为一维字节数组,反之亦然?

[英]How can I convert a Bitmap to a 1D byte array and vice versa in C#?

I wrote the following methods, 我写了以下方法,

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, 和,

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, 他们似乎没有工作,

在此处输入图片说明

What has been the problem do you think? 您认为出了什么问题?

NB NB

Driver program, 驱动程序

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

Okay. 好的。

I have solved it. 我已经解决了。

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

        public static Bitmap ByteArray1dToBitmap(byte[] bytes, int width, int height)
        {
            PixelFormat pixelFormat = PixelFormat.Format8bppIndexed;
            Bitmap bitmap = new Bitmap(width, height, pixelFormat);

            // Set the palette for gray shades
            ColorPalette pal = bitmap.Palette;
            for (int i = 0; i < pal.Entries.Length; i++)
            {
                pal.Entries[i] = Color.FromArgb(i, i, i);
            }
            bitmap.Palette = pal;

            BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, pixelFormat);
            int colorDepth = Bitmap.GetPixelFormatSize(pixelFormat);
            int noOfChannels = colorDepth / 8;

            unsafe
            {
                byte* address = (byte*)bitmapData.Scan0;
                int area = width * height;
                int size = area * noOfChannels;
                for (int i = 0; i < area; i++)
                {
                    address[i] = bytes[i];//262144 bytes
                }
            }

            //////////////////////////////////////////////////////////////
            bitmap.UnlockBits(bitmapData);

            return bitmap;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM