Groups | Blog | Home
all groups > dotnet drawing api > may 2006 >

dotnet drawing api : Wrong palette of BMP Image (if gray ok)



Matteo
5/24/2006 3:22:41 AM
Hi,
i sent image byte via bt from j2me phone to c# application,
after i have to create image using this code:

....
byte[] pixels
....

Bitmap bmp = new Bitmap(160, 120, PixelFormat.Format8bppIndexed);

BitmapData d = bmp.LockBits(new Rectangle(0, 0, 160, 120),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

// image data is pixel
Marshal.Copy(pixels, 0, d.Scan0, pixels.Length);

bmp.UnlockBits(d);

// Grey-Scale Palette works
/*
ColorPalette cp = bmp.Palette;
for (int i = 0; i < cp.Entries.Length; i++)
cp.Entries[i] = Color.FromArgb(i, i, i);
bmp.Palette = cp;
*/

this.pictureBox1.Image = bmp;

bmp.Save("C:\\img.bmp");

-----------------

The image that i save has all wrong color...
If i use other than Format8bppIndexed i have smaller size image
repeated on 160x120 pixel with color wrong too

Can someone helps me?

(sorry for bad english)

Thanks
Michael Phillips, Jr.
5/24/2006 9:32:23 AM
Bitmaps are dword aligned. You must account for these dword boundaries with
every scanline that you copy.

For example:
// bitmaps are upside down
IntPtr startOfScanline =
(IntPtr)(d.Scan0.ToInt32()+((d.Height-1-scanlineNo)*d.Stride));


[quoted text, click to view]

Matteo
5/25/2006 7:57:13 AM
Sorry im not very skilled using image and byte and dont undestand u
in wich way can i fix my code using your hint?
thanks a lot!
Matteo

Michael Phillips, Jr. ha scritto:

[quoted text, click to view]
Michael Phillips, Jr.
5/25/2006 1:42:55 PM
Use a loop to copy one scanline at a time where each scanline consists of
d.Stride bytes aligned on a dword boundary.

For a 8bpp Indexed bitmap, each byte represents an index into the Palette.

[quoted text, click to view]

AddThis Social Bookmark Button