nothing is being drawn in the forms paint event

T

Tony Johansson

Hello!

Here I have a piece of code that doesn't draw anything when I have the code
marked with 1 but works perfect when I use the code that is marked with 2.
Can somebody explain that ?

private void Form1_Paint(object sender, PaintEventArgs e)
{
1 Bitmap bm = new Bitmap(600, 600);
1 Graphics g = Graphics.FromImage(bm);

2 Graphics g = this.CreateGraphics();

Brush b = new LinearGradientBrush(new Point(1, 1), new Point(600,
600),
Color.White, Color.Red);
Point[] points = new Point[] {new Point(10,10),
new Point(77,500),
new Point(590, 100),
new Point(250,590),
new Point(300,410)};
g.FillPolygon(b, points);
g.DrawPolygon(new Pen(Color.Red, 7), points);
}

//Tony
 
K

kndg

Hello!

Here I have a piece of code that doesn't draw anything when I have the code
marked with 1 but works perfect when I use the code that is marked with 2.
Can somebody explain that ?

private void Form1_Paint(object sender, PaintEventArgs e)
{
1 Bitmap bm = new Bitmap(600, 600);
1 Graphics g = Graphics.FromImage(bm);

You are drawing on the bitmap, so nothing will be drawn on the form.
2 Graphics g = this.CreateGraphics();

Don't do that. Use

Graphics g = e.Graphics;

instead.
 
J

Jeff Johnson

2 Graphics g = this.CreateGraphics();

Clearly you didn't follow that link to Bob Powell's site I gave you in the
other thread or you'd know that CreateGraphics() is almost never what you
want.

Seriously, go to Bob's site. His FAQ will cover just about every pitfall
you're going to stumble into as you learn about GDI+ graphics in .NET.
 

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