Graphics painting VB.NET

  • Thread starter =?iso-8859-1?B?RGFm/A==?=
  • Start date
?

=?iso-8859-1?B?RGFm/A==?=

Hello!

I have an application that paint graphics in VB.NET.
Each graphic is painted after a long calculation process.

My problem is when another windows pass over my graphics
or I resize the window all graphs disapear.

How can I maintain the graphics?

I use to draw the graphics lines, boxes of the namespace

System.Drawing

Thanks for the answer
 
H

Herfried K. Wagner [MVP]

* "Dafü said:
I have an application that paint graphics in VB.NET.
Each graphic is painted after a long calculation process.

My problem is when another windows pass over my graphics
or I resize the window all graphs disapear.

How can I maintain the graphics?

I use to draw the graphics lines, boxes of the namespace

Create a bitmap with the appropriate size first ('Dim b As New
Bitmap(...)'). Then get a 'Graphics' object for this bitmap using
'Graphics.FromImage'. Use this object to draw onto the bitmap and paint
the bitmap onto the form in the form's 'Paint' event handler.

Maybe you want to subscribe to Bob Powell's "Well Formed":

<http://www.bobpowell.net/August2003.htm>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
A

_Andy_

Hello!

I have an application that paint graphics in VB.NET.
Each graphic is painted after a long calculation process.

My problem is when another windows pass over my graphics
or I resize the window all graphs disapear.

How can I maintain the graphics?

I use to draw the graphics lines, boxes of the namespace

System.Drawing

Thanks for the answer

Actual drawing should take place in the OnPaint event handler of the
object you're painting onto (eg. a Form). When you require complex
drawing, as you seem to indicate, it's best to pre-process as much as
possible somewhere else, so only a very limited amount of calculation
is performed in the OnPaint. A normal place to do this is in the same
place you calculate the resizing of the Form (control, or whatever).

Having said that, you can find the part of the Form that has been
uncovered by another window by looking at the invalidated rectangle.
You can just redraw the contents of that rectangle to preserve the
output. However, sometimes this itself is so complex that the amount
of time it takes to workout what the contents of the invalidated
rectangle should be, that it would actually be quicker to redraw the
whole page.

So, to summarise (and this is for all Windows applications, not just
VB.NET):

1) Perform as much of the calculation as possible in Form_Resize. This
can include complex mathematical computation, scale factors etc.

2) Fonts etc. (and their heights etc.) can be loaded or discovered in
Form_Load, although it is sometimes useful to put this into
Form_Resize instead.
eg. When a font or some text changes, it might be necessary to
calculate the size of the rectangle it takes to draw - a natural place
for this is in a separate function (called from Form_Resize and in the
property handler of the Font in question).

3) Perform all drawing in the Form_Paint method. This will be all the
Graphics.Draw* methods etc. Ideally, you should concentrate on the
implementation here to make it operate as fast as possible. Also, you
should try to avoid things like painting the same point more than
once. On virtually all occasions, you can abandon the handling of clip
rectangles (ie. be lazy) to no noticeable decrease in performance.

4) The code for (1) and (2) can be called by yourself at any time. The
Form_Resize method is called by the framework before the first Paint
event is fired.

Rgds,
 
A

Armin Zingler

Dafü said:
Hello!

I have an application that paint graphics in VB.NET.
Each graphic is painted after a long calculation process.

My problem is when another windows pass over my graphics
or I resize the window all graphs disapear.

How can I maintain the graphics?

I use to draw the graphics lines, boxes of the namespace

System.Drawing

Thanks for the answer

Whenever the OS wants you to paint the window, you should paint it. This can
be done in the control's paint event, or by "overriding" OnPaint.
 
A

Anand[MVP]

This used to be so easy in VB6, with Autoredraw. But now
you are on your own..:)

Whenever you form is redrawn you have to redraw all
additional stuff you want. Easiest is to use a bitmap as
Herfried pointed out, but some people also put all their
redraw logic in the OnPaint.

Regards,
Anand
VB.NET MVP
http://manand.typepad.com
 

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