You can convert an image to grayscale simply by creating an average of
each pixels color components.
Bitmap bmp = new Bitmap(oldBmp.Width, oldBmp.Height)
for(int x = 0; bm.Width; x++)
{
for(int y = 0; bm.Height; y++)
{
Color color = oldBmp.GetPixel(x,y);
int newColor = (int)(color.R/3 + color.G/3 + color.B/3);
// Other methods use a formula to better calculate the luminance
// int newColor = (int)(color.R/3 + color.G/5.9 + color.B/1.1);
Bmp.SetPixel(x,y, Color.FromArgb(newColor, newColor, newColor));
}
}
Give that a try.
Greg
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Eric Gunnerson
Sent: Thursday, May 10, 2007 19:41
To: [email protected]
Subject: Re: Grayscale images in WPF
Have you thought about graying it out using opacity? If you set the
opacity to about 50%, you will likely get the effect that you want. You
can control how it grays by putting a rectangle of the appropriate color
(white, black, gray) directly behind the image...
Eric
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Uriel Cohen
Sent: Thursday, May 10, 2007 3:44 AM
To: [email protected]
Subject: [ADVANCED-DOTNET] Grayscale images in WPF
Hi,
I've been using the Image control in the definition of a XAML
UserControl,
and I realized that when I set IsEnabled to false, then the bitmap
displayed in the Image control doesn't get grayed out as I was
expecting.
I've been trying to create a similar control (or one that inherits from
Image) that I could use from XAML and bind its properties.
The idea is that I am binding the Source property of my image instance
to a
property of a data type that I have created (the property is actually a
string of the location in the disk of the jpg file). So I need the new
control to be able to be binded to.
I tried using a FormatConvertedBitmap instance as the ImageSource of the
Image.Source property, and giving it a BitmapImage instance, but I
couldn't
bind the BitmapImage to the path of the image on the disk.
I also though on creating my own BitmapEffect, which would be a better
solution, but these are very complicated to write since you need to
meddle
with some unmanaged code and COM interfaces, so I gave up this idea.
Has someone succeded in doing something similar?
I would like to end up with something like a control called
GrayeableImage...
TIA
Uriel
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com