开发者

Getting RGB array from image in C#

I'm currently writing a C# implementation of a little program which I have written in Java.

I had used 开发者_运维问答BufferedImage.getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) function in my Java app. But I couldn't exactly find a version of this in C# and I am not sure how to write it manually.


There's not a direct equivalent in the .NET Framework to this method. However, if your image is a System.Drawing.Bitmap, you can call the LockBits method, and this will return a BitmapData structure that contains the address of the first scanline. You can then use it to create what should be an API-compatible wrapper. I'm assuming you're using C# 3.5 or greater, so I'm using an extension method - if you're using an older flavor, change this to a regular method by dropping the 'this' from the Bitmap argument:

    public static void getRGB(this Bitmap image, int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
    {
        const int PixelWidth = 3;
        const PixelFormat PixelFormat = PixelFormat.Format24bppRgb;

        // En garde!
        if (image == null) throw new ArgumentNullException("image");
        if (rgbArray == null) throw new ArgumentNullException("rgbArray");
        if (startX < 0 || startX + w > image.Width) throw new ArgumentOutOfRangeException("startX");
        if (startY < 0 || startY + h > image.Height) throw new ArgumentOutOfRangeException("startY");
        if (w < 0 || w > scansize || w > image.Width) throw new ArgumentOutOfRangeException("w");
        if (h < 0 || (rgbArray.Length < offset + h * scansize) || h > image.Height) throw new ArgumentOutOfRangeException("h");

        BitmapData data = image.LockBits(new Rectangle(startX, startY, w, h), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat);
        try
        {
            byte[] pixelData = new Byte[data.Stride];
            for (int scanline = 0; scanline < data.Height; scanline++)
            {
                Marshal.Copy(data.Scan0 + (scanline * data.Stride), pixelData, 0, data.Stride);
                for (int pixeloffset = 0; pixeloffset < data.Width; pixeloffset++)
                {
                    // PixelFormat.Format32bppRgb means the data is stored
                    // in memory as BGR. We want RGB, so we must do some 
                    // bit-shuffling.
                    rgbArray[offset + (scanline * scansize) + pixeloffset] = 
                        (pixelData[pixeloffset * PixelWidth + 2] << 16) +   // R 
                        (pixelData[pixeloffset * PixelWidth + 1] << 8) +    // G
                        pixelData[pixeloffset * PixelWidth];                // B
                }
            }
        }
        finally
        {
            image.UnlockBits(data);
        }
    }

This wrapper can now be called like this:

        Bitmap foo = Bitmap.FromFile(@"somefile.jpg") as Bitmap;
        int[] rgbArray = new int[100];
        foo.getRGB(1, 1, 10, 10, rgbArray, 0, 10);

Hope this helps, and welcome to .NET!


You'd use Bitmap.LockBits to get direct access to the pixels in a bitmap. Here's a sample implementation, it returns one scanline from the passed bitmap as an int[]:

    int[] getRGB(Bitmap bmp, int line) {
        var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
            System.Drawing.Imaging.ImageLockMode.ReadOnly,
            System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        try {
            var ptr = (IntPtr)((long)data.Scan0 + data.Stride * (bmp.Height - line - 1));
            var ret = new int[bmp.Width];
            System.Runtime.InteropServices.Marshal.Copy(ptr, ret, 0, ret.Length * 4);
            return ret;
        }
        finally {
            bmp.UnlockBits(data);
        }
    }


I think the closest one is Bitmap.GetPixel(x,y) that return a single pixel color at a point. In order to simulate the java function, you will need to write some helper.


You may need to check

  • Bitmap.GetPixel
  • Bitmap.LockBits

Also check Converting an array of Pixels to an image in C#.


It depends how fast you need to do it.

Bitmap has GetPixel() method which works fine for a pixel.

If you need to do fast image processing you need to use LockBits which you can find a sample here.

Bitmap img = (Bitmap) Image.FromFile(imageFileName);
BitmapData data = img.LockBits(new Rectangle(0,0,img.Width, img.Height), ImageLockMode.ReadWrite, img.PixelFormat);
byte* ptr = (byte*) data.Scan0;
for (int j = 0; j < data.Height; j++)
{
    byte* scanPtr = ptr + (j * data.Stride);
    for (int i = 0; i < data.width; i++, scanPtr+=NO_OF_CHANNELS)
    {
        for (int m = 0; m < NO_OF_CHANNELS; m++)
            Console.WriteLine(*scanPtr); // value of each channel
    }
}

img.UnlockBits(data);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜