The logic behind converting image to another color table with unsa

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

I need to do an application that takes a Bitmap object and convert it to one
of the color tables below:

1. 16 colors.
2. 256 colors.
3. 24bpp.

I read about lockbits and unsafe code, but how can I change the actual value
of the pixels according to the pallets? how do I set their values? there is a
formula for red, blue and green?

I just need the theory/idea. then I will convert it myself.

thx.
 
TomHL,

You can use the GetPixel and SetPixel methods on the Bitmap class.
However, this will be VERY slow, so you might want to opt for using unsafe
code and the LockBits method, which will give you direct access to the bits
that make up the bitmap.

You have to be careful here, since you need to be able to read/write the
current bitmap format correctly (based on stride and whatnot). Once you
call LockBits, you will have a pointer to that area in memory (or rather, an
IntPtr, which you can convert to a pointer in unsafe code).

Hope this helps.
 
See the LockBits article in the GDI+ FAQ that explains the different
BitmapData layouts for the various pixel formats.

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

Ramuseco Limited .NET consulting
http://www.ramuseco.com

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

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

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