Quantcast
Channel: paint event on picturebox
Viewing all articles
Browse latest Browse all 48

paint event on picturebox

$
0
0

In vb.net we must draw on a graphics surface, which is basically a memory block, or on a bitmap, which is basically a memory block, and then assign the bitmap to a graphics surface. The system then will copy the memory block to the video card memory and then it gets updated and shown on the screen (monitor).

So, the graphics surface is just a memory area that gets copied to the screen. The memory gets copied to the screen at 64 hertz more or less ie every 1/64 of a second the graphics surface memory is copied to the screen video memory and shown in the monitor by the system.

When your draw on a graphic surface like e.graphics it is constantly being update and then you the programmer or the system needs to redraw everything to show it again.

In vb6 this was the job of AUTOREDRAW.

In vb.net it is your job to cause the surface to be redrawn as require by letting the system call the paint event or by coding the event using control.refresh.

We can cause the redraw several ways. One is cover the surface ie picture box and uncover it. This uncovering makes the system call the control paint event. When the paint event is called by the system it supplies the control graphics surface or handle or memory address to the paint event subroutine through the e.graphics variable or the "PaintEventArgs."

If you put your code in the paint event, then every time it needs to be redrawn the system will call the event and execute the code in the paint event handler subroutine.

You can see in the example two lines are drawn every time the screen is updated by the system in the paint event. Cover the example with another application form and uncover it the paint event is called and all the event code is executed.

Therefore, the answer to your question is draw in the paint event on e.graphics and draw all of your graphics objects from within the event sub routine. Then every time the event is called both lines are drawn.

If one wants to force the system to repaint or call the paint event then use the statement control.refresh or control.invalidate.

Comprende?

This information has been explained many times on this forum and on the web if you just search for it.

:)

Make the example and run it. Add a break to the first clear statement. Cover the form and uncover it. Single step the event code. When you understand this let us know and we will show the next step.

That means you Enzo and Hany both.

Public Class Form5
    Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

        e.Graphics.Clear(Color.White)

        e.Graphics.DrawLine(Pens.Red, 20, 20, 200, 20)

        e.Graphics.DrawLine(Pens.Red, 110, 20, 110, 200)

    End Sub
End Class



Viewing all articles
Browse latest Browse all 48

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>