Solved

Refreshing graphics on a picture box

Posted on 2004-08-17
7
262 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now