Transparency in Bitmaps

Posted by Jeffrey Vanneste at 09:53 PM on February 25, 2004

Category: Development

I've started working on my new game although I'm not quite sure what it will be in the end. I'm pretty sure it will be a puzzle game on a board made of hexagons (I have a couple hexagon-ish games in mind) so I've just been working on drawing hexagons so far. I originally tried just using DrawPolygon to draw the polygons but they looked so boring so I figured I'll make up some nice looking hexagon bitmap and just draw that. Well, I haven't ever had to draw a bitmap with transparency so I looked into that tonight. Apparently the MakeTransparent method isn't available in the Compact Framework so I had to see if there was another way to do it. My last resort was to loop through all the pixels of the bitmap and turn the colors that I wanted to be transparent manually but thankfully I don't have to do that. Anyways, using the ImageAttributes class it was quite easy to specify which color to be transparent. So here it is:

Graphics g = e.Graphics;
Bitmap hexagon = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TransparencyTest.hex.bmp"));
System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();
Color transparentColor = hexagon.GetPixel(0,0);
ia.SetColorKey(transparentColor, transparentColor);

// draw hexagon with transparent background
g.DrawImage(hexagon, new Rectangle(50, 25, hexagon.Width, hexagon.Height), 0, 0, hexagon.Width, hexagon.Height, GraphicsUnit.Pixel, ia);

// draw hexagon without transparent background
g.DrawImage(hexagon, 50, 100);

This draws the following bitmaps on the screen:

notTransparent.png

transparent.png

That's pretty much it. My sample project can be dowloaded here. Later I'll post some code on drawing the hexagon so they are connected properly. I found the secrect is multiples of 4. Till then, enjoy.

RCS to CVS

Posted by Jeffrey Vanneste at 09:30 PM on February 09, 2004

Category: Development

Well tonight I'm making the change from Component Software RCS to a linux CVS server and using Tortoise CVS for the front end. Originally I used CS-RCS at work and they had a free single user version so I used that one at home. Well, I'm pretty much fed up with the CS-RCS file explorer and the shell extensions (not to mention I was just using the RCS capabilities of it) so I decided I would install the CVS server on my mandrake linux box here and try out Tortoise CVS as recommended to by a friend. So that's my evening, moving all my source code over to the CVS server. Fun fun.