[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

How to determine grayscale image ?

Andrew

12/23/2004 2:43:00 PM

Hello.

How to determine that a loaded Bitmap is grayscale ?

I am loading a grayscale JPEG using System.Drawing.Bitmap.FromFile() and
PixelFormat property is Format8bppIndexed. It's strange to me, that
there is no something like Format8bppGrayScale in GDI+ (there is only
Format16bppGrayScale, which not likely to use for me). So, GDI+ creates
a table with 256 entries with colors from black to white. But, loading,
for example, GIF gives the same result - Format8bppIndexed. So, how to
determine, that I was loaded a grayscale image ? I was trying to use
Bitmap.Palette.Flags property, to determine, because MSDN says, that
"0x00000002 - the colors in the array are grayscale values". But the
flag is equal to zero.

Andrew
3 Answers

RB

12/23/2004 4:48:00 PM

0

Perhaps you could loop through the 256 entries in the pallette, and confirm
that each entry corresponds with a grey level? For example, when I create an
8bpp greyscale image, I build the palette entries as follows:

<code>
Bitmap tmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);

ColorPalette greyPal = tmp.Palette;

for(int i = 0;i<greyPal.Entries.Length;i++)
greyPal.Entries[i] = Color.FromArgb(i,i,i);
</code>

Not sure if the above method of building palette entries is a strict
definition of grayscale...

Norvin



"Andrew" <andrew@postmet.com> wrote in message
news:ecHBR3P6EHA.1120@TK2MSFTNGP11.phx.gbl...
> Hello.
>
> How to determine that a loaded Bitmap is grayscale ?
>
> I am loading a grayscale JPEG using System.Drawing.Bitmap.FromFile() and
> PixelFormat property is Format8bppIndexed. It's strange to me, that
> there is no something like Format8bppGrayScale in GDI+ (there is only
> Format16bppGrayScale, which not likely to use for me). So, GDI+ creates
> a table with 256 entries with colors from black to white. But, loading,
> for example, GIF gives the same result - Format8bppIndexed. So, how to
> determine, that I was loaded a grayscale image ? I was trying to use
> Bitmap.Palette.Flags property, to determine, because MSDN says, that
> "0x00000002 - the colors in the array are grayscale values". But the
> flag is equal to zero.
>
> Andrew


Andrew

12/24/2004 8:42:00 AM

0

Hello, Norven.

Yes, I can do it. But it looks like work around of a GDI+ lack. Working
with libjgpeg, for example, you can exactly determine, that the image is
grayscale. But in GDI+ there is no such possibility.

Thanx

Norvin Laudon wrote:
> Perhaps you could loop through the 256 entries in the pallette, and confirm
> that each entry corresponds with a grey level? For example, when I create an
> 8bpp greyscale image, I build the palette entries as follows:
>
> <code>
> Bitmap tmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
>
> ColorPalette greyPal = tmp.Palette;
>
> for(int i = 0;i<greyPal.Entries.Length;i++)
> greyPal.Entries[i] = Color.FromArgb(i,i,i);
> </code>
>
> Not sure if the above method of building palette entries is a strict
> definition of grayscale...
>
> Norvin

Bob Powell

12/24/2004 8:50:00 AM

0

You could look at the saturation of the pixels. If they are all desaturated,
they are all gray.

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

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

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

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





"Andrew" <andrew@postmet.com> wrote in message
news:ecHBR3P6EHA.1120@TK2MSFTNGP11.phx.gbl...
> Hello.
>
> How to determine that a loaded Bitmap is grayscale ?
>
> I am loading a grayscale JPEG using System.Drawing.Bitmap.FromFile() and
> PixelFormat property is Format8bppIndexed. It's strange to me, that
> there is no something like Format8bppGrayScale in GDI+ (there is only
> Format16bppGrayScale, which not likely to use for me). So, GDI+ creates
> a table with 256 entries with colors from black to white. But, loading,
> for example, GIF gives the same result - Format8bppIndexed. So, how to
> determine, that I was loaded a grayscale image ? I was trying to use
> Bitmap.Palette.Flags property, to determine, because MSDN says, that
> "0x00000002 - the colors in the array are grayscale values". But the
> flag is equal to zero.
>
> Andrew