Transparency Control

G

gwoodhouse

Hello all,

Ive been trying to make a control which will allow me to draw
transparent masks over whatever is behind the control irrelivant of
what that control/image may look like.

My implimentation at the very basic level has a 2d byte array, each
element of the array corresponding to the opacity of the "Cell" at
that location.

On mouse move the redraw method is called:
{
offScreenGraphics.Clear(Color.Transparent);
for (int x = 0; x < GRIDLENGTH; x++)
for (int y = 0; y < GRIDHEIGHT; y++)
{
SolidBrush solidBrush = new
SolidBrush(Color.FromArgb((opacity[x, y] * 62), Color.White));
offScreenGraphics.FillRectangle(solidBrush,
this.Left + (x * CELLWIDTH), this.Top + (y * CELLHEIGHT), CELLWIDTH,
CELLHEIGHT);
}

this.CreateGraphics().DrawImage(memoryBitmap, new
Rectangle(0, 0, memoryBitmap.Width, memoryBitmap.Height), 0, 0,
memoryBitmap.Width, memoryBitmap.Height, GraphicsUnit.Pixel);
}

This gives me the problem of having the correct amount of opacity
drawn every move of the mouse, basically making the opacity layers
stack until each "Cell" is a completly opaque.

I'm wondering if the Invalidate() method could be used to "Clear" the
previous layer before drawing the new one, but Invalidate() only
carries itself out on the end of a paint method and i have no idea how
to force a blank paint command. See the below code for an example
{
Invalidate();
// Force a paint cycle here before the .DrawImage() method
is called.
offScreenGraphics.Clear(Color.Transparent);
...
this.CreateGraphics().DrawImage(memoryBitmap, new
Rectangle(0, 0, memoryBitmap.Width, memoryBitmap.Height), 0, 0,
memoryBitmap.Width, memoryBitmap.Height, GraphicsUnit.Pixel);
}

Any help would be greatly appreciated.
 
C

Chris Dunaway

to force a blank paint command. See the below code for an example
{
Invalidate();
// Force a paint cycle here before the .DrawImage() method
is called.
offScreenGraphics.Clear(Color.Transparent);
...
this.CreateGraphics().DrawImage(memoryBitmap, new
Rectangle(0, 0, memoryBitmap.Width, memoryBitmap.Height), 0, 0,
memoryBitmap.Width, memoryBitmap.Height, GraphicsUnit.Pixel);

}

Any help would be greatly appreciated.

Invalidate will cause the Paint event to be fired, so you could clear
the offscreen buffer and then invalidate. Move the painting code to
the paint event. You should not call this.CreateGraphics() as you are
doing, because you are creating a graphics object every time and then
not disposing of it.

Instead, you should do your painting in the paint event because the
paintEventArgs class has a Graphics object that you should use to do
your painting. Instead of this.CreateGraphics().DrawImage(...), you
would use e.Graphics.DrawImage(...).

See this site for additional information:

www.bobpowell.net

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top