0

How can we get the info from an image if it contains by calculating the no of pixels and there arrangement.

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90

3 Answers3

2

You can make use of the Bitmap class in .Net

Here a nice example: Image Processing for Dummies with C# and GDI+ Part 1 - Per Pixel Filters

Also the following StackOverflow (Question: How to manipulate images at pixel level in C#) provides already an answer :D

Community
  • 1
  • 1
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
0

You can use the Bitmap (System.Drawing) class. Then, to manipulate pixels, use the methods GetPixel() and SetPixel(). But if you want a faster access to the pixels, read this article: Using the LockBits method to access image data by Bob Powell that exploits pointers, compiling with unsafe code.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Omar
  • 16,329
  • 10
  • 48
  • 66
0

Here's an example of manipulating pixel data using pointers.

unsafe
{
int red, blue, green;
editImage = new Bitmap("image.jpg");
editWidth = editImage.Width;
editHeight = editImage.Height;
data = editImage.LockBits(new Rectangle(0, 0, editWidth, editHeight), 
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* dataPtr = (byte*)data.Scan0;
h = trackBar1.Value / 60.0;
D = 1 - Math.Abs((h % 2) - 1);

if (h >= 0 && h < 1)
{
    for (int i = 0; i < editHeight; i++)
    {
        offsetStride = i * data.Stride;
        for (int j = 0; j < editWidth; j++)
        {
            blue = dataPtr[(j * 3) + offsetStride];
            green = dataPtr[(j * 3) + offsetStride + 1];
            red = dataPtr[(j * 3) + offsetStride + 2];

            if (green > blue) max = green;
            else max = blue;
            if (red > max) max = red;

            if (green < blue) min = green;
            else min = blue;
            if (red < min) min = red;

            s = (max == 0) ? 0 : 1d - (1d * min / max);
            v = max / 255d;

            C = v * s;
            X = C * D;
            E = v - C;

            dataPtr[(j * 3) + offsetStride] = (byte)(min);
            dataPtr[(j * 3) + offsetStride + 1] = (byte)((X + E) * 255);
            dataPtr[(j * 3) + offsetStride + 2] = (byte)(max);
        }
    }
}
}

This just changes the hue of an image. For any reasonably large image > 100x100 pixels, don't bother using GetPixel or SetPixel unless performance doesn't matter.

Jack
  • 5,680
  • 10
  • 49
  • 74