graphics, drawing problem

  • Thread starter Thread starter Jeroen Ceuppens
  • Start date Start date
J

Jeroen Ceuppens

Hi,

When I draw a bitmap on a form, the bitmapdrawing disappears when I move the
form, how is it possible to lock the drawing so it stays visible when you
move or do somethings else....

Thx
Jeroen
 
When I draw a bitmap on a form, the bitmapdrawing disappears when I move
the
form, how is it possible to lock the drawing so it stays visible when you
move or do somethings else....

Have you tried implementing the Paint() event? Redrawing would be easier
than trying to "lock" the image.
 
Jeroen,

The way that windows works is that there is a loop which does nothing
but process messages. One of these messages is a command which indicates
that the window should repaint itself. When this happens, in .NET usually
the OnPaint method of a Control class is called (which is exposed in a Form,
because Form extends Control).

Now, if you call a method to paint a bitmap in a button click, the image
will appear, but the next time the window gets a notification to re-paint
itself, it will dissapear, because there are no instructions to keep the
bitmap painted (you did it once in the button click, and the OnPaint method
or the Paint event handler doesn't have any knowledge of your bitmap).

The paint routine is really a state machine. You should override
OnPaint. In it, you call the base implementation. Then, you check the
state. If a bitmap exists to paint, then paint it, otherwise, do nothing.

Now, in your button click (or wherever you set the bitmap), you would
load the picture, and then store it where the OnPaint method can access it.
Then, you would call the Invalidate method. This will send a message to
your window to repaint itself, at which point, your custom code in OnPaint
will paint the loaded bitmap.

Hope this helps.
 
Hi Jeroen,

Set the bitmap as a backgound image.
Control.BackgroundImage. The framework will take care of drawing the bitmap
on the form

HTH
B\rgds
100
 
Thx!

I put in these code: and the bmp keeps there! I have a few questions about
it, what does the base.OnPaint(e) mean?

private void button1_Click(object sender, System.EventArgs e)

{

button1_clicked=true;

bmp=new Bitmap(@"\\geronimo\im\ImageWeftIllum.bmp");

this.Invalidate();

}

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

if (button1_clicked==true)

g.DrawImage(bmp,10,10);


}



Nicholas Paldino said:
Jeroen,

The way that windows works is that there is a loop which does nothing
but process messages. One of these messages is a command which indicates
that the window should repaint itself. When this happens, in .NET usually
the OnPaint method of a Control class is called (which is exposed in a Form,
because Form extends Control).

Now, if you call a method to paint a bitmap in a button click, the image
will appear, but the next time the window gets a notification to re-paint
itself, it will dissapear, because there are no instructions to keep the
bitmap painted (you did it once in the button click, and the OnPaint method
or the Paint event handler doesn't have any knowledge of your bitmap).

The paint routine is really a state machine. You should override
OnPaint. In it, you call the base implementation. Then, you check the
state. If a bitmap exists to paint, then paint it, otherwise, do nothing.

Now, in your button click (or wherever you set the bitmap), you would
load the picture, and then store it where the OnPaint method can access it.
Then, you would call the Invalidate method. This will send a message to
your window to repaint itself, at which point, your custom code in OnPaint
will paint the loaded bitmap.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeroen Ceuppens said:
No i didn't, what do you mean by implementing?
A sort of repaint?

when
you
 
base.OnPaint calls the base class implementation. In this case it does no
drawing but it does raise the Paint event.

Leaving it out may not break your code but it may prevent other people who
use your component from doing custom painting of their own.

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Jeroen Ceuppens said:
Thx!

I put in these code: and the bmp keeps there! I have a few questions about
it, what does the base.OnPaint(e) mean?

private void button1_Click(object sender, System.EventArgs e)

{

button1_clicked=true;

bmp=new Bitmap(@"\\geronimo\im\ImageWeftIllum.bmp");

this.Invalidate();

}

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint (e);

if (button1_clicked==true)

g.DrawImage(bmp,10,10);


}



Nicholas Paldino said:
Jeroen,

The way that windows works is that there is a loop which does nothing
but process messages. One of these messages is a command which indicates
that the window should repaint itself. When this happens, in .NET usually
the OnPaint method of a Control class is called (which is exposed in a Form,
because Form extends Control).

Now, if you call a method to paint a bitmap in a button click, the image
will appear, but the next time the window gets a notification to re-paint
itself, it will dissapear, because there are no instructions to keep the
bitmap painted (you did it once in the button click, and the OnPaint method
or the Paint event handler doesn't have any knowledge of your bitmap).

The paint routine is really a state machine. You should override
OnPaint. In it, you call the base implementation. Then, you check the
state. If a bitmap exists to paint, then paint it, otherwise, do nothing.

Now, in your button click (or wherever you set the bitmap), you would
load the picture, and then store it where the OnPaint method can access it.
Then, you would call the Invalidate method. This will send a message to
your window to repaint itself, at which point, your custom code in OnPaint
will paint the loaded bitmap.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jeroen Ceuppens said:
No i didn't, what do you mean by implementing?
A sort of repaint?

"Frecklefoot" <Chris_nospam@nospam_nsageotech.com> schreef in bericht
When I draw a bitmap on a form, the bitmapdrawing disappears when
I
move
the
form, how is it possible to lock the drawing so it stays visible when
you
move or do somethings else....

Have you tried implementing the Paint() event? Redrawing would be easier
than trying to "lock" the image.
 
Back
Top