tags:

views:

47

answers:

2

I want to draw a shape to the alpha channel only of my image. I can use a colour matrix when drawing an image with Graphics.DrawImage(..), as it has a parameter to pass in the ImageAttributes. Is it possible to do this with the FillEllipse method? Or would I have to draw it to a separate image, then use DrawImage to apply it to my main image?

Cheers, Dan.

A: 

You may try something like:

Graphics g;
g.CompositionMode = CompositingMode.SourceOver;

then fill with:

Color c = Color.FromArgb(100, Color.White);
sinm
That just seems to premultiply white with (100/255), before writing the colour to the image's colour channels.
Dan
+1  A: 

I wrote a blog post on this a couple of years ago:

http://danbystrom.se/2008/08/24/soft-edged-images-in-gdi/

In short: you have to first draw your shape to a separate bitmap and then transfer one of its RGB channels over to the destination's alpha channel.

danbystrom
I ended up storing the information in a separate image. I only wanted to store it in the alpha channel to be more efficient with both storage, and my image data analysis code (which performs computation against the colour channels and my values I'm storing in the alpha channel). I'm not actually rendering using alpha-blending when drawing the image onto the screen. Might switch over to your method when I get time though - saves me messing around with multiple image buffers.
Dan