drawing on a panel

S

Saso Zagoranski

Hi!

I have three transparent panels on my form. The three panels represent three
layers.
I would like to draw to those panels but since Panel doesn't support
CreateGraphics()
I create a Bitmap:
bitmap = new Bitmap(sizeX,sizeY);
Get the Graphics object of that bitmap:
Graphics g = Graphics.FromImage(bitmap);

Then I draw to g and in the end I call the panel.Invalidate() method:
private void layerOne_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap,0,0);
}

layerOne is the first panel. The problem is that when I call the
layerOne.Invalidate() method
the Paint event handler isn't even called?

How can I fix this problem?

One other thing... Is there a better way of creating layers of graphics
object? I would like to
have a map on the base layer and than some small circles on the second layer
and some other stuff
(not yet sure what) on the third one.
Is using panels the right way of doing this?

thanks,
saso
 
B

Boris Nienke

private void layerOne_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap,0,0);
}

the Paint event handler isn't even called?

i think there's a "override"-statement missing?
One other thing... Is there a better way of creating layers of graphics
object? I would like to
have a map on the base layer and than some small circles on the second layer
and some other stuff
(not yet sure what) on the third one.
Is using panels the right way of doing this?

i don't think so but of course it depends... :)

As you said, you have a bitmap on every panel... so the whole application
is just displaying bitmaps (in this area where the panels are). Now you
would like to simulate 3 Layers.

Here is what i would do to get this:

1.) Create a Bitmap "bmpFinal". This is your virtual screen where you paint
everything

2.) create one Bitmap for each Layer. You can make a Collection of Bitmaps
to have unlimited layers (the user could then add, delete, rearange
layers). You are doing something like that allready!

3.) Do all painting for a layer (and for all layers if needed); Then make a
loop to paint all layers to the "bmpFinal". Of course you will need some
kind of "Blueboxing" to make parts transparent (look at the attributes when
using Bitmap.Draw or similiar)

4.) Display the "bmpFinal" as you wish (for example paint it to a PaintBox)


This way you won't have to deal with panels (which are controls that needs
extra time to create) etc.

Depending on the graphic-routines you are also able to use Alpha-Blending
between layers (to let one shine thru if you like) or any other effekt you
like... You have every layer in memory so you can do whatever you like with
it... duplicate it - enable/disable it (by redrawing the "bmpFinal" and
skipping the disabled ones etc.

I would recommend to make a new Layer-Class (with some properties and the
Bitmap) could be easier to handle

....i've written a Graphic-Application (Photo-Manipulation) with Delphi
using this technique. I've optimized the redrawing so i can move one layer
around and a layer is just that big as the max-rectangle of the content -
so if you just have a small circle, the bitmap of the layer is small too.
It should be possible to the same technique with .Net
....but i don't know of _really_ fast bitmap manipulation tools (to have
alpha blending and stuff)

Boris
 
S

Saso Zagoranski

Thanks for answering...

I think your suggestion is very good. I have already created the finalBmp
and two layer Bitmaps.

I place a map on the first layer (and in this case on the form)
Bitmap layerOne = new Bitmap(100,100);
Graphics g = Graphics.FromImage(layerOne);
g.DrawImage(...);
Graphics formG = MyForm.CreateGraphics();
formG.DrawImage(layerOne,0,0);

What if I now wanted to draw another image on the form? The second image
would just have a few dots on it everything else should be transparent...
how do I do this?
 
S

Saso Zagoranski

I made a function with bottomLayer and upperLayer as parameters.
In this function I go through each of the pixels in the upperLayer and if
the pixel
is white than I color it:
upperLayer.SetPixel(x,y,bottomLayer.GetPixel(x,y));

This works but it is REALLY slow!
 
B

Boris Nienke

NO Don't use "SetPixel" Pixel - it takes ages to finish!!!

you can copy one bitmap to another with one color transparent. Don't have
the code here - but it has to do with "Attributes" ... Look out for the
Splash-Screen-Sample (animated bitmaps with transparency) or others... just
scan this group a bit.

Or look at the sample "Owner Draw ListBox" - there's a way to draw bitmaps
with transparency too (to draw an icon for each item)

Sorry that i don't can offer you links - but i'm not at home where my links
are

Boris
 

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