user3198066
user3198066

Reputation: 13

Pixel searcher on a photo

I've got some photos that I want to do some fun things with.

For example, I've got this one pohto with lots of colours blended in, and I want to be able to dissect the photo into pixels and to check what colour each pixel is, so I can hold record of exactly what colour is used the most in the photo.

Is there any library built in that I can use that has the capacity to dissect such a photo and iterate through every pixel?

Upvotes: 1

Views: 58

Answers (2)

fejesjoco
fejesjoco

Reputation: 11903

You can load your images with Image.FromFile. If they are small, you can call Bitmap.GetPixel in a loop. If they are big, more than a few megapixels I guess, it will be very slow, then you should use Bitmap.LockBits instead. All links have samples. You can use a Dictionary collection to build a histogram, just be careful because similar pixels will have slightly different R, G and B values, so your collection might grow incredibly huge.

Upvotes: 1

user2046117
user2046117

Reputation:

Load the image into an BitMap then process the bitmap using GetPixel method which returns a Color object

  private void GetPixel_Example(PaintEventArgs e)
    {

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");
    int width = myBitmap.Width;
    int height = myBitmap.Height;


    // Get the color of a pixel within myBitmap.

    for (int widthIndex = 0;widthIndex < width;widthIndex++) 
    {
        for (int heightIndex = 0;heightIndex < height;heightIndex ++) 
        {
              Color pixelColor = myBitmap.GetPixel(widthIndex, heightIndex);
              // add to dictionary
        }
    }
    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
    }

You can use a dictionary to store the colour and the count

Upvotes: 0

Related Questions