Solved

Refreshing graphics on a picture box

Posted on 2004-08-17
7
266 Views
Last Modified: 2008-02-26
Hello

I have a picture box where i display graphics using gdi functions.

Dim g As Graphics
g = Pic_Dessin.CreateGraphics
...
and so on....

The graphics are redrawn anytime i change a value in the textboxes on the same form.
I want the picture to displayed at all time.

I tried to solutions :

1- Draw on loading and on form activation (MyBase.Activated, MyBase.GotFocus events)
Problem : Image tends to disapear for no reason after loading, and occasionally, when i click on a textbox (this seems pretty random for me)

2- Use a timer to refresh the drawing every 800 ms
Problem : each time the graphics are redrawn, the picture box goes white for a moment, which doesn't look too good...

I'm out of ideas now, and i will appreciate help ^^
Thx in advance

0
Comment
Question by:SweetNenya
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 11821903
You need to draw your items in the Picbox's PAINT event.  The Paint event will pass an "e" argument which will give you access to the graphics object in the picturebox as:

e.Graphics

Use this to do your drawing.  When you want to update the image, simple execute PictureBox1.Invalidate() which will force the repaint.

Jeff
0
 

Author Comment

by:SweetNenya
ID: 11822104
First, let me be sure i got this right :

Private Sub Dessin(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Pic_Dessin.Paint
         'Drawing function here
End Sub

Now, at loading, the graphics are drawned, erased, and redrawn (all this is quite fast, so it doesn't matter too much)
Even though, for some (unknown) reason, when i pressed TAB to go from one textbox to another, the drawing disappear... any idea why?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11822141
Or let your redrawing be handled by the mybase.activated event, and the textbox textchanged

Private Sub RedrawPicture(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated, txtColor1.TextChanged ' etc...



end sub
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 14

Accepted Solution

by:
ptakja earned 200 total points
ID: 11822207
Come to think of it, if you are drawing your own images using GDI+, the picturebox is overkill.  You can draw on any surface using GDI+.  Have you tried drawing to a Panel control?  The Picbox has a ton of overhead designed to make image manipulation a breeze.  You aren't utilizing any of that (at least as far as I can tell from your post).

Try replacing your Picbox with a Panel and handle the Panel's paint event and do your drawing in there.
0
 

Author Comment

by:SweetNenya
ID: 11822243
Hmm, this is what i described in my "solution 1" in my first post...

Actually, i think the problem could be easy to solve if i knew why the image is erased... but i have no idea =/
0
 

Author Comment

by:SweetNenya
ID: 11822575
I replaced the picture box by a panel, and the graphics works fine now ^^
I would never tried that by myself, thx a lot ptakja
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11822614
Your welcome.  Glad I could help!  

Jeff
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question