Getting RGB array from image in C#

前端 未结 5 1115
面向向阳花
面向向阳花 2020-12-10 06:06

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

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 06:49

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

提交回复
热议问题