[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

microsoft.public.dotnet.framework.drawing

Finding nearest color of a predefined palette

Retoro

1/24/2005 3:38:00 AM

I have a seemingly simple problem that I've been unable to find the solution
for.

I have 15 sets of predefined palettes, each having 15 colors. All I'm
trying to do, is load images (jpg, gif, or bmp) or any size into a 32x32
15-color image. I know how to resize the image to a 32x32 bitmap, but then
I want this image to be "fitted" into these 15-color palettes, using the
nearest matching color for each pixel from a given palette.

I think I know how to build my palette for my bitmap:

Public Function GetPaletteOfIndex(ByVal iPaletteIndex As Integer) As
ColorPalette
Dim bmp As Bitmap = New Bitmap(1, 1, PixelFormat.Format8bppIndexed)
Dim pal As ColorPalette = bmp.Palette

Dim i As Integer
For i = 0 To 14
'clrPalette is a preset 15x15 array of Color objects
pal.Entries(i) = clrPalette(iPaletteIndex, i)
Next

'set the 16th (unused) Entry to transparent
pal.Entries(15) = Color.FromArgb(0)
bmp.Dispose()
Return pal
End Function

I'm just not sure where to go from here. I've tried loading an image into
the bitmap predefined w/ this palette, but I think instead of it using the
predefined palette, it overwrites it w/ the palette from the file. I've
also tried drawing an image into the bitmap w/ the predefined palette, but
you can't use Graphics from an 8bpp bitmap.

I've read this article:
http://support.microsoft.com/...

.... but it looks like it's calculating the "nearest color" on its own (and
this example is using a grayscale palette). Plus, the memory stuff there
(LockBits, Scan0, Stride) kinda loses me -- surely all that isn't necessary
for what I'm trying to do.

I've searched on the Graphics.GetNearestColor function, which seems like it
would be what I need, but I'm unsure what it's getting the nearest color
*of*. It would be perfect if this function was a member of the ColorPalette
object, and it would just return the closest match for that color in that
palette, but alas, this function is on the Graphics object (which can't be
obtained on anything other than a 32bpp bitmap). So I'm not even sure in
what case I would use the GetNearestColor function.

So, is what I'm trying to do really that difficult to accomplish? I would
think this would be a fairly common practice, and not that difficult to pull
off.

Thanks so much in advance for any help.

Jerad


2 Answers

Bob Powell

1/24/2005 11:53:00 AM

0

There is an article in the GDI+ FAQ that explains how to modify the
transparent colour in a palette. This shows you how to alter the palette in
any 8 bpp image. You should be able to adapt this for your needs. There is
also an article on LockBits that explains how that works if you're
interested.

--
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.





"Jerad Rose" <no@spam.com> wrote in message
news:eJEfmYcAFHA.1296@TK2MSFTNGP10.phx.gbl...
>I have a seemingly simple problem that I've been unable to find the
>solution
> for.
>
> I have 15 sets of predefined palettes, each having 15 colors. All I'm
> trying to do, is load images (jpg, gif, or bmp) or any size into a 32x32
> 15-color image. I know how to resize the image to a 32x32 bitmap, but
> then
> I want this image to be "fitted" into these 15-color palettes, using the
> nearest matching color for each pixel from a given palette.
>
> I think I know how to build my palette for my bitmap:
>
> Public Function GetPaletteOfIndex(ByVal iPaletteIndex As Integer) As
> ColorPalette
> Dim bmp As Bitmap = New Bitmap(1, 1, PixelFormat.Format8bppIndexed)
> Dim pal As ColorPalette = bmp.Palette
>
> Dim i As Integer
> For i = 0 To 14
> 'clrPalette is a preset 15x15 array of Color objects
> pal.Entries(i) = clrPalette(iPaletteIndex, i)
> Next
>
> 'set the 16th (unused) Entry to transparent
> pal.Entries(15) = Color.FromArgb(0)
> bmp.Dispose()
> Return pal
> End Function
>
> I'm just not sure where to go from here. I've tried loading an image into
> the bitmap predefined w/ this palette, but I think instead of it using the
> predefined palette, it overwrites it w/ the palette from the file. I've
> also tried drawing an image into the bitmap w/ the predefined palette, but
> you can't use Graphics from an 8bpp bitmap.
>
> I've read this article:
> http://support.microsoft.com/...
>
> ... but it looks like it's calculating the "nearest color" on its own (and
> this example is using a grayscale palette). Plus, the memory stuff there
> (LockBits, Scan0, Stride) kinda loses me -- surely all that isn't
> necessary
> for what I'm trying to do.
>
> I've searched on the Graphics.GetNearestColor function, which seems like
> it
> would be what I need, but I'm unsure what it's getting the nearest color
> *of*. It would be perfect if this function was a member of the
> ColorPalette
> object, and it would just return the closest match for that color in that
> palette, but alas, this function is on the Graphics object (which can't be
> obtained on anything other than a 32bpp bitmap). So I'm not even sure in
> what case I would use the GetNearestColor function.
>
> So, is what I'm trying to do really that difficult to accomplish? I would
> think this would be a fairly common practice, and not that difficult to
> pull
> off.
>
> Thanks so much in advance for any help.
>
> Jerad
>
>


Retoro

1/24/2005 3:22:00 PM

0

Thanks Bob for your response.

I will be checking out your site, starting w/ the GDI+ Beginners Guide,
since I still feel fairly new at all this. I'm typically a business
developer and have not had much exposure to the graphical side of .NET --
though, I'm enjoying it quite a bit.

If you don't mind, keep an eye on this thread over the next week or so, so
that I may hit you up w/ other questions I may have after reading the guides
you suggested.

Thanks again for your help.

Jerad

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:uC1nVtgAFHA.3824@TK2MSFTNGP10.phx.gbl...
> There is an article in the GDI+ FAQ that explains how to modify the
> transparent colour in a palette. This shows you how to alter the palette
in
> any 8 bpp image. You should be able to adapt this for your needs. There is
> also an article on LockBits that explains how that works if you're
> interested.
>
> --
> 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.
>
>
>
>
>
> "Jerad Rose" <no@spam.com> wrote in message
> news:eJEfmYcAFHA.1296@TK2MSFTNGP10.phx.gbl...
> >I have a seemingly simple problem that I've been unable to find the
> >solution
> > for.
> >
> > I have 15 sets of predefined palettes, each having 15 colors. All I'm
> > trying to do, is load images (jpg, gif, or bmp) or any size into a 32x32
> > 15-color image. I know how to resize the image to a 32x32 bitmap, but
> > then
> > I want this image to be "fitted" into these 15-color palettes, using the
> > nearest matching color for each pixel from a given palette.
> >
> > I think I know how to build my palette for my bitmap:
> >
> > Public Function GetPaletteOfIndex(ByVal iPaletteIndex As Integer) As
> > ColorPalette
> > Dim bmp As Bitmap = New Bitmap(1, 1,
PixelFormat.Format8bppIndexed)
> > Dim pal As ColorPalette = bmp.Palette
> >
> > Dim i As Integer
> > For i = 0 To 14
> > 'clrPalette is a preset 15x15 array of Color objects
> > pal.Entries(i) = clrPalette(iPaletteIndex, i)
> > Next
> >
> > 'set the 16th (unused) Entry to transparent
> > pal.Entries(15) = Color.FromArgb(0)
> > bmp.Dispose()
> > Return pal
> > End Function
> >
> > I'm just not sure where to go from here. I've tried loading an image
into
> > the bitmap predefined w/ this palette, but I think instead of it using
the
> > predefined palette, it overwrites it w/ the palette from the file. I've
> > also tried drawing an image into the bitmap w/ the predefined palette,
but
> > you can't use Graphics from an 8bpp bitmap.
> >
> > I've read this article:
> > http://support.microsoft.com/...
> >
> > ... but it looks like it's calculating the "nearest color" on its own
(and
> > this example is using a grayscale palette). Plus, the memory stuff
there
> > (LockBits, Scan0, Stride) kinda loses me -- surely all that isn't
> > necessary
> > for what I'm trying to do.
> >
> > I've searched on the Graphics.GetNearestColor function, which seems like
> > it
> > would be what I need, but I'm unsure what it's getting the nearest color
> > *of*. It would be perfect if this function was a member of the
> > ColorPalette
> > object, and it would just return the closest match for that color in
that
> > palette, but alas, this function is on the Graphics object (which can't
be
> > obtained on anything other than a 32bpp bitmap). So I'm not even sure
in
> > what case I would use the GetNearestColor function.
> >
> > So, is what I'm trying to do really that difficult to accomplish? I
would
> > think this would be a fairly common practice, and not that difficult to
> > pull
> > off.
> >
> > Thanks so much in advance for any help.
> >
> > Jerad
> >
> >
>
>