+ Reply to Thread
Results 1 to 2 of 2

Thread: RGB colors in C#

  1. #1

    RGB colors in C#

    I have a picturebox with a button to load image into the picturebox .
    I want to load a bitmap image into the picturebox and then perform some operation on pixel ranges rgb(110,120,130) to rgb(192,168,110) of the loaded image.
    Last edited by basemhosam; 01-16-2012 at 05:56 PM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    Simple way would be something like this:
    Code:
    for (int i = 0; i < width; i++)
       for (int j = 0; j < height; j++)
       {
           Color c = bitmap.GetPixel(i, j);
           if (ColorWithinRange(c))
           {
               // do anything here
           }
       }
    With ColorWithinRange defined like this:
    Code:
    private readonly Color _from = Color.FromRgb(110, 120, 130);
    private readonly Color _to = Color.FromRgb(192, 168, 110);
    bool ColorWithinRange(Color c)
    {
       return 
          (_from.R <= c.R && c.R <= _to.R) &&
          (_from.G <= c.G && c.G <= _to.G) &&
          (_from.B <= c.B && c.B <= _to.B);
    }
    By the way , if the image is big , then GetPixel and SetPixel become very slow .

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts