Bitmap.Save is increasing number of colors

P

Piotrekk

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 anyone tell me where I am going wrong.

regards
PK
 
J

Jesse Houwing

Hello Piotrekk,
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 anyone tell me where I am going wrong.
regards
PK

What is the format you're using to save the bitmap? If you're saving it as
a JPG the compression algorithm might choose a number of different colours
to suit the compression level better.
 
P

Peter Duniho

Piotrekk said:
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 anyone tell me where I am going wrong.

You should convert the bitmap to an actual palettized format before
saving. As Jesse said, just because you've reduced the number of unique
colors before saving a full 24-bit JPEG, that doesn't mean that the
resulting file will be left with that number of unique colors. In fact,
JPEG is a terrible format for palettized images.

You need to reduce the number of colors (as you're already doing),
create a new 8-bit palettized bitmap (see
PixelFormat.Format8bppIndexed), set the palette to match the colors
you've chosen, and copy the original image into the new bitmap. When
you save the new bitmap, save it as a BMP format file (see
ImageFormat.Bmp), and that should preserve your color selection.

There might be a way to even use RLE compression, but I haven't looked
into that.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top