# [.net] Bitmap - integer values (indices) instead of Color

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, When I load a 16-color bitmap into a Bitmap object, and use the GetPixel() method, I get a Color object. Is there some simple, quick way to get the integer value (which would correspond to the palette index here) of a pixel? It would really suck if I had to obtain the palette of that bitmap and search for the R,G,B values just to get the damn index. thx, unshaven

##### Share on other sites
i would suggest locking the bitmap, gettin its bmpdata structur, copying out the data from scan0 into your target type array, and gettin ur index/color from there. its not hard to do just look it up in the msdn documentation that came with your copy of the dotnet sdk. and there is no need to use the unsafe context like a lot of people use.

##### Share on other sites
Quote:
 Original post by UnshavenBastardIt would really suck if I had to obtain the palette of that bitmap and search for the R,G,B values just to get the damn index.

I don't think it's that huge of a pain, really. Just make a small utility function once and never worry about it again. :P

	int GetEntryFromPixel(Bitmap B, int x, int y)	{		Color C = B.GetPixel(x,y);		for(int a=0; a < B.Palette.Entries.Length; a++)		{			if(B.Palette.Entries[a] == C)				return a;		}		return 0;	}

I hope that helps you out. :)

##### Share on other sites
renzime,

hehe yes that's what I did, finally, thanks anyway.
but I still don't like that I could not access it directly...

to the other poster:
of course I could do that, but that doesn't change the fact that it sucks *g*
I don't like such inefficient things when it would be so easy with direct access to the values originally loaded from the bitmap file, instead of Color objects...
maybe that really doesn't matter in this little business app here that I was given by my boss as practising project (I'm learning .NET / C#, for this will be "the new thing" that we'll be using in the future, and I started here as a kind of "trainee"), but still I don't like it, deep inside me there's something complaining ;-)

##### Share on other sites
btw, renzime,

you said I wouldn't need the unsafe context.
how that?
I got an IntPtr from this function, casted it to byte to acces the data, and that need's the unsafe context.

How would you've done it?

I would like to have no unsafe in my stuff, if not necessary.

##### Share on other sites
by unsafe context i ment using Unsafe { } blocks all you do is a simple copy using the Marshal.copy method works both ways as well.

small example here :) (needs rest of code filling in)

// what ever ur data type is
byte[] bytearray = new byte[datasize];

Marshal.Copy(bmpdata.Scan0,bytearray,0,datasize);

you could even create a class for this, storing the data in an array, with functions such as getpixel and setpixel for modifying it. ( i do this in an few projects i have on the go "in a super stupid gl demo doing terrains and set state liquid surfaces with it" )

##### Share on other sites
ah,

thankyou for the Marshal.Copy() thingy,
I was already peeking around in the "System" namespace for some "ArrayCopy" like you have in JAVA, 'cause I thought this might be without need of the unsafe, now you showed the equivalent's name to me, thanks :D

btw, I was afraid I won't like C#, because I've read it's supposed to be similar to JAVA, I love C++ and find JAVA sooo extremely boring to program in (I feel like it takes my creativity, puts it in a tiny box and repeatedly jumps on it for half an hour to make it fit in there...)
but now, since I'm using C# for... like 4 weeks or so, I must say, particularly in developing windows apps, but also programming in it in general is just like a dream!
Though I hope they'll add templates soon... :D
I think I'm gonna do all my tools etc. in C#/.NET in the future, and use C++ only for "speedy stuff"...

• 21
• 16
• 9
• 17
• 13