C# 将位图转换为RGB像素

C# 将位图转换为RGB像素,c#,bitmap,C#,Bitmap,我正在使用以下代码,并尝试将位图转换为0-255个RGB像素的数组。在下面的代码中,我对伪代码有一些注释,需要帮助: private class Rgb { public byte Red { get; set; } public byte Green { get; set; } public byte Blue { get; set; } public Rgb(byte red, byte green, byte

我正在使用以下代码,并尝试将位图转换为0-255个RGB像素的数组。在下面的代码中,我对伪代码有一些注释,需要帮助:

    private class Rgb
    {
        public byte Red { get; set; }
        public byte Green { get; set; }
        public byte Blue { get; set; }

        public Rgb(byte red, byte green, byte blue)
        {
            Red = red;
            Green = green;
            Blue = blue;
        }
    }

    private class MyImage
    {
        private Rgb[] _pixels;
        private readonly int _width;
        private readonly int _height;

        public int Width
        {
            get { return _width; }
        }

        public int Height
        {
            get { return _height; }
        }

        public Rgb[] Pixels
        {
            get { return _pixels; }
        }

        public MyImage(int width, int height)
        {
            _width = width;
            _height = height;

            _pixels = new Rgb[_width*_height];
        }

        public static MyImage FromBitmap(Bitmap bmp)
        {
            var myImage = new MyImage(bmp.Width, bmp.Height);

            int i = 0;
            // foreach(pixel in myImage)
            // {
            //     myImage._pixels[i] = new Rgb(pixel.red, pixel.green, pixel.blue);
            //     i++;
            // }

            return myImage;
        }

        public static Bitmap ToBitmap(MyImage myImage)
        {
            var bitmap = new Bitmap(myImage.Width, myImage.Height);

            int i = 0;
            // foreach(pixel in myImage)
            // {
            //     bitmap.pixel[i].red = myImage.Pixels[i].Red;
            //     bitmap.pixel[i].green = myImage.Pixels[i].Green;
            //     bitmap.pixel[i].blue = myImage.Pixels[i].Blue;
            //     i++;
            // }

            return bitmap;
        }
我一直在研究如何从位图中获取像素数据。此外,我需要能够反过来从像素数据创建位图。任何帮助都将不胜感激

看看这个链接: 注意24位的图像,因为如果我记得很清楚的话,它们需要将行对齐到4字节。

看看CodeProject文章,你可以从那里开始。也可以看看。