My Blog Links
Site Links
Currently Reading
  • This page is powered by Blogger. Isn't yours?
  • Weblog Commenting and Trackback by HaloScan.com
  • 2RSS.com :: RSS directory
  • Listed on BlogShares
  • Blog Directory, Find A Blog, Submit A Blog, Search For The Best Blogs

Tuesday, March 02, 2004

Drawing Composite Image... 

You know some things are just harder than they should be. I was trying to draw an Image so that it would paint with the selection color, as needed. That way, if the item was selected, its background would composite draw with the selection. You've probably seen this type of functionality in all sorts of WinForms controls. Since a picture is worth a 1000 words, here goes:

My control had an ImageList attached. On the ImageList, the TransparentColor was set to white. So, I thought if I used ImageList.Draw, it would paint it correctly given the TransparentColor. That way when an item was selected, the image would paint with the selection color for the bits of the image that had the transparent color.

Well, needless to say, that didn't work. I had to call the right incarnation of Graphics.DrawImage (mainly one that took an ImageAttributes as a parameter). By setting the right ColorKey in the ImageAttributes, it finally painted correctly. Here's the code:

Color lowerColor = Color.FromArgb(250,250,250);
Color upperColor = Color.FromArgb(255,255,255);
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(lowerColor, upperColor, ColorAdjustType.Default);

g.DrawImage(imageList.Images[imageIndex], rect, 0, 0,
imageList.ImageSize.Width, imageList.ImageSize.Height,
GraphicsUnit.Pixel, imageAttr );

Comments: Post a Comment