Sign in to follow this  

[.net] C# - Drawing with layers...?

This topic is 3584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have created a standard form with a pictureBox, and i want to create some simple graphics (just shapes mostly, maybe a small image) above the pictureBox. I know how to draw these, but the problem is, i can't get them to be displayed above the pictureBox. I need to know how I can control the layers of the Graphics I am creating, or control the order in which they are being Painted on the form...??? Thanks in Advance, gilly914.

Share this post


Link to post
Share on other sites
Things drawn earlier will appear below things drawn later. If you are overriding OnPaint in your picture box and you want to draw on top of the image in the picture box make sure to call base.OnPaint() before you do your drawing. There is no implied layering in a standard graphics context, things simply show up from back to front in the order you provide them.

hope this helps

Share this post


Link to post
Share on other sites
You don't need the picture box at all. The picture box is just a simple means of showing a picture that you have stored on disk. It's better to just circumvent it completely.


using System;
using System.Drawing;
using System.Windows.Forms;

namespace GeneticPaint
{
public partial class Form1 : Form
{
Graphics front, back;
Bitmap buffer;
Timer t;
public Form1()
{
InitializeComponent();
buffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
back = Graphics.FromImage(buffer);
front = this.CreateGraphics();
t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 1;
}

void t_Tick(object sender, EventArgs e)
{
t.Stop();
//Do all your drawing to the "back" graphics object
//Draw in order from back to front, this is called "Painter's Algorithm"
back.Clear(Color.CornflowerBlue);
back.DrawRectangle(Pens.Red, 10, 10, 20, 30);
back.Flush();
//"flip" the back to the front, this is called double buffering
front.DrawImageUnscaled(buffer, ClientRectangle.X, ClientRectangle.Y);
front.Flush();
t.Start();
}
}
}





There are other issues of timing that are not covered in this example

Share this post


Link to post
Share on other sites
Thanks capn_midnight!!!

I think this example really helps me!...
I'll try it, and get back to you, although, can you direct me to good source to learn the very very basics o these concepts??? Double buffering? layered drawing? etc...

Thanks Again,
gilly914

Share this post


Link to post
Share on other sites

This topic is 3584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this