?
Solved

Applying operands to a Color object

Posted on 2006-05-08
10
Medium Priority
?
203 Views
Last Modified: 2010-04-16
Hi,

I have a bitmap containing red pixels, but when I attempt to change just the pixels which are red, I have trouble accessing them.  I clearly see the values changing between the blank, transparent, pixels.  They have an RGB=0x0.  And the non-blank pixels have a value of 0xffff0000.  But I'm having trouble relating those values to the various Color values.

for ( int x=0; x<bmp.Width; x++ )
{
   for ( int y=0; y<bmp.Height; y++ )
   {
      Color color = bmp.GetPixel( x, y );
      if ( color == Color.Red )
      {
                         // trying to get into this loop
      }
   }
}

Also, I tried testing for blank, but that does not work either, even though the value of color displays at 0x0.

      if ( color == Color.Transparent )
      {
                         // trying to get into this loop
      }

Has anyone got a suggestion?

Thanks,
Bob
0
Comment
Question by:ba272
10 Comments
 
LVL 11

Accepted Solution

by:
Agarici earned 800 total points
ID: 16630358
try to check just the rgb values like:

if( color.R == Color.Red.R && color.B == Color.Red.B && color.G == Color.Red.G )
{
 //...
}

but i think you should consider comparing to a range of values because it is very dificult for a pixel in a real-world image to have the exact same rgb value for red as Color.Red, excepting the case you have set them yourself.

hth,
A.
0
 

Author Comment

by:ba272
ID: 16631424
Could you please give me an example of using color value ranges?  I agree this is a good approach.

Thanks a lot,
Bob
0
 
LVL 11

Assisted Solution

by:vo1d
vo1d earned 800 total points
ID: 16633649
what agarici ment was something like this:

if(color.R > 127 && color.R < 200)
{


}

that statement checks for a red part between 128 and 199.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ba272
ID: 16633677
thanks.  Is there a table somewhere which gives the ranges of these colors?
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16633892
what do you mean with ranges of colors?
there are 2^32 colors, a table for all that would be too large.
the ranges for each color component(rgb) is between 0 - 255, each component at 128 is grey.
you could check out the color chooser of windows to check out some values.

0
 

Author Comment

by:ba272
ID: 16633913
ok.  where do I find a color chooser?  not sure what you mean...
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16633992
right click on your desktop, choose properties, then click appearance and try to change a color for e.g. texthighlight.then the rgb values will be shown.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16634089
one more question, do you wanna compute large images?
i only ask because the get and set pixel methods are very slow.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 400 total points
ID: 16636036
Bob,

You could speed this up with some GDI+ magic:

Unsafe Image Processing
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp11152001.asp

Example:

public void MakeGreyUnsafe()
{
    Point size = PixelSize;
    LockBitmap();

    for (int x = 0; x < size.X; x++)
    {
        for (int y = 0; y < size.Y; y++)
        {
            PixelData* pPixel = PixelAt(x, y);

            int value = (pPixel->red + pPixel->green + pPixel->blue) / 3;
            pPixel->red = (byte) value;
            pPixel->green = (byte) value;
            pPixel->blue = (byte) value;
        }
    }
    UnlockBitmap();
}

Bob
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16650411
your could speed that method up if you write an int to the whole pixel instead of writing each byte for r,g,b.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question