Solved

Refreshing graphics on a picture box

Posted on 2004-08-17
7
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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