473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting images

Using C# 2005

How can I load a 24bit .BMP image and convert it in a 8bit BMP,
and save the new one on disk?

Many Thanks,
Filippo

Mar 9 '06 #1
3 1614
The first thing you're going to have to do is come up with some business
rule that determines how to reduce the colors from 24-bit to 8-bit. There
will definitely be loss of color in the process.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Filippo Bettinaglio" <Fi****************@discussions.microsoft.com> wrote
in message news:BA**********************************@microsof t.com...
Using C# 2005

How can I load a 24bit .BMP image and convert it in a 8bit BMP,
and save the new one on disk?

Many Thanks,
Filippo

Mar 9 '06 #2

I wanna achive excacly what Microsoft paint does:

it is like if I load a bmp image 24bit and then I can select "save as..."
and the change the format to 8bit colors.
Many thanks,
Filippo

"Kevin Spencer" wrote:
The first thing you're going to have to do is come up with some business
rule that determines how to reduce the colors from 24-bit to 8-bit. There
will definitely be loss of color in the process.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Filippo Bettinaglio" <Fi****************@discussions.microsoft.com> wrote
in message news:BA**********************************@microsof t.com...
Using C# 2005

How can I load a 24bit .BMP image and convert it in a 8bit BMP,
and save the new one on disk?

Many Thanks,
Filippo


Mar 9 '06 #3
> I wanna achive excacly what Microsoft paint does:

it is like if I load a bmp image 24bit and then I can select "save as..."
and the change the format to 8bit colors.
Microsoft Paint achieves what it does by virtue of a programmer who wrote
the application. You want to write an application. You're the programmer.

Unfortunately, I don't know how Microsoft Paint does the color conversion. I
don't believe Microsoft has published that information. So, you're still
going to have to figure it out for yourself.

I can give you a few clues. A 24-bit format is RGB (red, green, blue) color.
This means that each pixel is 3 bytes, each of which represents a value from
0 - 255, and that indicates the intensity of the color (red, green, or
blue). This means that a pixel can represent any of 16,777,216 (256*256*256)
colors.

An 8-bit format is quite a bit different. It stores color information in a
single byte, having (of course) a value from 0 - 255. This means that a
pixel can represent any of 256 colors. What those 256 colors are is
determined by the use of a palette, an color mapping table which specifies
the 24-bit color of each of the 256 possible color values. These 256 colors
can be any 256 colors you want them to be, but they have to be 256 specific
RGB color values.

To convert an image from 24-bit to 8-bit therefore requires a series of
steps. The first is to sample the 24-bit image, and use an algorithm to
change the color of each pixel to one of 256 possible color values. How this
algorithm works is entirely up to the developer. There are good ones and bad
ones. One of the easiest (but not the best) is to use the standard 256
web-safe color palette, determine the closest match in that palette for each
pixel, and convert it to that color. Note that this is *not* easy, but just
one of the easiest ways to do this. It entails using a lot of math to
determine the hue, saturation, and luminance (HSL) of each 24-bit pixel,
find the hue, saturation, and luminance of the closest match in the web
pallette, and convert the result to RGB.

The drawback to this method becomes apparent when you realize that a 24-bit
image could be all shades of (for example) blue. When sampled using this
method, it loses much of its resolution, and becomes virtually meaningless,
since there are only a few dozen shades of blue in the web-safe color
palette.

A better method is to determine an arbitrary 256 colors that represent more
closely approximations of the "average" colors in the original image. This
again, involves a lot of math, as well as understanding how RGB and HSL are
related. Using this method, all pixels in the original image must be
compared to all other pixels in the image, to result in a 256-color "average
palette" of the colors in the original image. Each pixel can then be
converted to one of these 256 colors.

These sorts of algorithms, by virtue of their difficulty to create, are
usually closely-guarded by the developers. If you can find some, you're a
very fortunate fellow. You may be able to find a code library that has these
built into it. That may be your best bet.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
"Filippo Bettinaglio" <Fi****************@discussions.microsoft.com> wrote
in message news:13**********************************@microsof t.com...
I wanna achive excacly what Microsoft paint does:

it is like if I load a bmp image 24bit and then I can select "save as..."
and the change the format to 8bit colors.
Many thanks,
Filippo

"Kevin Spencer" wrote:
The first thing you're going to have to do is come up with some business
rule that determines how to reduce the colors from 24-bit to 8-bit. There
will definitely be loss of color in the process.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Filippo Bettinaglio" <Fi****************@discussions.microsoft.com>
wrote
in message news:BA**********************************@microsof t.com...
> Using C# 2005
>
> How can I load a 24bit .BMP image and convert it in a 8bit BMP,
> and save the new one on disk?
>
> Many Thanks,
> Filippo
>


Mar 9 '06 #4

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

Similar topics

2
by: engwar1 | last post by:
I'm a .NET newbie and am beginning the process of converting an existing personal website to .NET from classic ASP. I'm assuming I want my aspx files in the root of my site in the same place as...
4
by: RichG | last post by:
I have an Access database/forms application that has images stored in it. I need to develop a new VB that utilizes Oracle or the file system as the underlying storage for these images. In order...
0
by: Mark Allen | last post by:
Hello, I am creating an RTF document server side for a report. However I am having problems converting images into the required RTF format. I am converting the image into a string (binary)...
2
by: engwar1 | last post by:
I'm a .NET newbie and am beginning the process of converting an existing personal website to .NET from classic ASP. I'm assuming I want my aspx files in the root of my site in the same place as...
3
by: Muddasir | last post by:
hi all i need to upload an image and if that image is coloured i need to convert it to black and white image ... till now i only uploaded images ... simple.. have no idea how to do this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.