【问题标题】:Converting a Bitmap into RGB Pixels将位图转换为 RGB 像素
【发布时间】:2014-04-16 17:25:13
【问题描述】:

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

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

我不知道如何从位图中获取像素数据。此外,我需要能够反过来从像素数据创建位图。任何帮助将不胜感激!

【问题讨论】:

  • 看看thisCodeProject的文章,你可以从那里开始。
  • 也可以看看this

标签: c# bitmap


【解决方案1】:

看看这个链接: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.aspx 小心 24 位图像,因为(如果我没记错的话)它们需要行对齐到 4 个字节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2016-05-30
    • 2021-12-17
    • 2010-12-14
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多