all groups > dotnet drawing api > february 2008 >
You're in the

dotnet drawing api

group:

How to draw something into an indexed image


How to draw something into an indexed image Norbert_Pürringer
2/14/2008 7:41:44 AM
dotnet drawing api:
Hello there,

I would like to draw something into an indexed bitmap. The problem is,
that the Graphics object may only be used for non-indexed images.

So I have to convert my indexed image into an nonindexed image in
order to be able to manipulate the image and then I have to reconvert
it to the old indexed format.

My problem is to reconvert the non-indexed image into an indexed
image. How is it possible?

I use following code:

private Bitmap ConvertToPixelFormat(Bitmap bitmap, PixelFormat
pixelFormat)
{
Rectangle rectangle = new Rectangle(0, 0, bitmap.Width,
bitmap.Height);

BitmapData bmpData = bitmap.LockBits(rectangle,
ImageLockMode.ReadWrite, bitmap.PixelFormat);
int byteCount = bmpData.Stride*bmpData.Height;
byte[] bytes = new byte[byteCount];
Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
bitmap.UnlockBits(bmpData);

Bitmap bmpNew = new Bitmap(bitmap.Width, bitmap.Height,
pixelFormat);
BitmapData bmpDataNew = bmpNew.LockBits(rectangle,
ImageLockMode.ReadWrite, pixelFormat);
Marshal.Copy(bytes, 0, bmpDataNew.Scan0, bytes.Length);
bmpNew.UnlockBits(bmpDataNew);
return bmpNew;
}

The non-indexed image has got the pixel format Format32bppRgb and the
indexed image is of type Format8bppIndexed. I think, the problem is,
that the bytes array, which saves 4 bytes for each pixel is too big
for the indexed image taking only one byte for each pixel.

Any advice for me?

Thanks,
Re: How to draw something into an indexed image Norbert_Pürringer
2/14/2008 8:30:07 AM
Hi Michael,

On 14 Feb., 17:13, "Michael Phillips, Jr."
[quoted text, click to view]
I.

really? I do not have a lots of experience in gdi. Do you know how to
draw an subimage at a certain pixel position into an indexed image via
gdi using C#?

That would be really great, if I don't need to convert the image in
two directions.

Thank you,
Re: How to draw something into an indexed image Michael Phillips, Jr.
2/14/2008 11:13:31 AM
[quoted text, click to view]

This is a limitation of gdiplus. You have no limitations, if you use GDI.

[quoted text, click to view]

A 32bpp image must be unpacked and then repacked into a 8bpp indexed image.

Each 8bpp pixel is an index into a palette that represents the colors of
your image.

You need to create a palette that best represents the colors present in your
32bpp image and then reindex your image to 8bpp.

You will choose the nearest palette index that matches the color of each
32bpp pixel from this palette.

There are many methods of creating a palette. One method ported to work
with .Net is the octree quantization algorithm.

See the following:
http://msdn2.microsoft.com/en-us/library/aa479306.aspx



[quoted text, click to view]

Re: How to draw something into an indexed image Michael Phillips, Jr.
2/14/2008 1:11:31 PM
[quoted text, click to view]

You may access any of the gdi functions through p-invoke.

See pinvoke.net for information as to how to type the gdi functions for use
with any .Net language.

Your indexed 8bpp bitmap must be created as a DIB.

You may use LoadImage to load your 8bpp image as a DIB.

See the following:
http://support.microsoft.com/kb/158898

Once your DIB is created, select the handle into a gdi device context and
you may use any of the gdi functions to manipulate your indexed image.

You may use BitBlt or any of its variations to draw a sub image or any
cropped area of an image of any type to your 8bpp indexed image.

There are many examples as to how to proceed with c# on MSDN,
codeproject.com or try a google search.


[quoted text, click to view]
Hi Michael,

On 14 Feb., 17:13, "Michael Phillips, Jr."
[quoted text, click to view]

really? I do not have a lots of experience in gdi. Do you know how to
draw an subimage at a certain pixel position into an indexed image via
gdi using C#?

That would be really great, if I don't need to convert the image in
two directions.

Thank you,
Norbert

AddThis Social Bookmark Button