473,658 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing Bitmap Pixels to Colors

Hi all,

I'm trying to compare a color returned by a Bitmap's GetPixel method to
a normal static color of the Color class. My problem: after setting a
pixel to a certain color, the color returned by GetPixel isn't
considered the same color anymore. Example code:

bmp.SetPixel(0, 0, Color.Blue);
Color cc = bmp.GetPixel(0, 0);

if (cc.Equals(Colo r.Blue))
{
MessageBox.Show ("Pixel is blue");
}
else
{
MessageBox.Show ("Pixel isn't blue");
}

....this program will always tell me that the pixel isn't blue. Is there
anything I'm doing wrong? What's the proper way to compare these colors?

Thanks in advance.
Nov 16 '05 #1
3 6420
The compare routine looks at other properties such as the KnownColor flag to
verify equality. The only way to check is to see if the R, G, B and, if
needed, A values match.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Typpo" <ot********@opt online.net> wrote in message
news:uu******** ******@TK2MSFTN GP09.phx.gbl...
Hi all,

I'm trying to compare a color returned by a Bitmap's GetPixel method to a
normal static color of the Color class. My problem: after setting a pixel
to a certain color, the color returned by GetPixel isn't considered the
same color anymore. Example code:

bmp.SetPixel(0, 0, Color.Blue);
Color cc = bmp.GetPixel(0, 0);

if (cc.Equals(Colo r.Blue))
{
MessageBox.Show ("Pixel is blue");
}
else
{
MessageBox.Show ("Pixel isn't blue");
}

...this program will always tell me that the pixel isn't blue. Is there
anything I'm doing wrong? What's the proper way to compare these colors?

Thanks in advance.

Nov 16 '05 #2
Bob Powell [MVP] wrote:
The compare routine looks at other properties such as the KnownColor flag to
verify equality. The only way to check is to see if the R, G, B and, if
needed, A values match.


Thanks. The following method works for me.

private bool IsSameColor(Col or first, Color second)
{
if (first.A.Equals (second.A)
&& first.R.Equals( second.R)
&& first.G.Equals( second.G)
&& first.B.Equals( second.B))
return true;
else
return false;
}
Nov 16 '05 #3
For speed optimization I would check Alpha last as most likely it is the
same in all pixels :)
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6033
by: James Dean | last post by:
I get the following message when i try to set a pixel in the bitmap.......whats wrong and how can i fix it....... "Additional information: SetPixel is not supported for images with indexed pixel formats." this is the code......... bitmap.SetPixel(biheight,biwidth,Color.Black);
4
23861
by: Ma Xiaoguang | last post by:
Dear ladies and gentlemen: How to access all the pixels of a bitmap with C#? I am working with Delphi before. In Delphi, you can use TBitmap.Scanline property to access all the pixels of a bitmap object. How to do this with C#? Any hints? Thank you very much. Best regards. Xiaoguang
3
1263
by: John Dann | last post by:
I've never had much cause to read up in detail on GDI+ but there's a small piece of a project where I need to add some annotation (let's say just some small filled rectangles as markers) to a pre-existing bitmap and then resave the bitmap. I've tried several ways of doing this but they either don't work or give errors. Anyone able to help me out please? The sort of thing I'm doing is: Dim MyBMP as Bitmap =...
3
1654
by: Dennis | last post by:
I create a bitmap of 12 pixels wide x 15 pixels high then create a graphics object from the bit map. When I draw a rectangle g.DrawRectangle(blackpen, new rectangle(0,0,12,15)), I get the top and side but the far side and bottom are not drawn. If I draw a rectangle g.DrawRectangle(blackpen, new rectangle(0,0,11,14) then all four sides are drarwn. I am having trouble understanding why I can't get a rectangle 12 pixels wide by 15 pixels...
9
5461
by: fripper | last post by:
I have a simple VB 2005 app that has a bitmap image and at one point I want to look at a particular pixel and see if it has a color value equal to the value of a color variable, c. For example: dim c as color c = color.red If bitmap(x,y) = c then
10
3297
by: pcnerd | last post by:
I'm a VB.NET newbie. I've created a program that plots pixels at random on the form. I have a 19" LCD monitor with a resolution set to 1280 by 1024. If you do the math, that means that there are over 1.3 million pixels. In one version of the program, the pixels start plotting at the upper-left & go down the form from top to bottom & left to right. In another version, the pixels are plotted at random. I have a 2.4 GHZ Athlon 64 with 1...
2
3858
by: Piotrekk | last post by:
Hi I have a problem. I open bitmap file, load it to memory decrease color palette by proceeding Euclidean distance. As a result i have image reduced to - for example 18 colors. Once i save it - Bitmap.Save(filename) reopen and check color of every pixel it seems that there are over 41000 of different pixel colors. I need to save it in such a way that there will be exactly 18 colors in a file. It seems i have problem with it... Can...
20
6997
by: Joe Duchtel | last post by:
Hello - I have the following code to get a bitmap from the clipboard and to save it to a *.png file ... Dim lData As IDataObject = Clipboard.GetDataObject() If lData.GetDataPresent(DataFormats.Bitmap) Then Dim lPictureBox As New PictureBox
3
3718
by: radu | last post by:
i need a bit of help with few lines of code. 1. i have integer array a = numbers from 0 to 256 2. i have to create a 256 colours bitmap of 500x500 pixels, asign colours as matrix a. ex. bitmap at pixel at row 3 col 5 = colour number is a and save it to disk as a 256 colour bitmap without any compresion as c:\a.bmp. no interface. i have microsoft visual c# 2008 express edition. help? thank you.
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.