 |
 |

07-20-2004, 06:21 PM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
ALMOST have permanent Graphics!!
|
Hi, I want to draw permanent graphics ("AutoRedraw" in VB 6, "Do-it-yourself" in VB.NET) on a PictureBox without putting graphics methods on the Paint event (because its inefficiency). I've read that this can be made by drawing not directly on the PictureBox graphics object but in the PictureBox image graphics object.
I made this function based on a function that I found in a forum:
Code:
Function CreatePermanentGraphics(ByVal cajaImagen As PictureBox) As Graphics
Dim BMPImage As New Bitmap(cajaImagen.Width, cajaImagen.Height)
If cajaImagen.Image Is Nothing Then
cajaImagen.Image = BMPImage
Else
Graphics.FromImage(BMPImage).DrawImage(cajaImagen.Image, 0, 0)
cajaImagen.Image = BMPImage
End If
Return Graphics.FromImage(BMPImage)
End Function
Quick explanation: First time, if the PictureBox doesn't have an Image yet, I give it a new one, else, the new image will be the one it has previously. Then, the function returns the "BMPImage" graphics object.
Ok, I have 2 buttons and a PictureBox called pct1:
Code:
Private G As Graphics
Private Sub cmdCreatePermanentGraphics_Click(...) Handles cmdCreatePermanentGraphics.Click
G = CreatePermanentGraphics(pct1)
End Sub
Private Sub cmdDraw_Click(...) Handles cmdDraw.Click
G.DrawLine(Pens.Chocolate, 0, 0, 50, 50)
G.DrawEllipse(Pens.Chocolate, 0, 0, 50, 50)
End Sub
This procedure works just OK:
Code:
Sub CrearYDibujar()
cmdCreatePermanentGraphics.PerformClick()
cmdDraw.PerformClick()
End Sub
It shows immediately the shapes drawn. BUT, if the user click those buttons in that order, nothing happens, and it is necessary to refresh explicitly the control in order to view the results of the drawing. WHY???? they are the SAME procedures being executed in the SAME order. The core of my program is the drawing speed, and I just cannot refresh the control each time I draw something.
I would really appreciate you help. Please, help! (yes, you!, the one who is sitted there, HELP ME!!!, please!)
|
|

07-22-2004, 08:28 AM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
What we ALL need
|
Hi,
Look, everybody should try the code I've attached. The graphics doesn't go away. Why?, simple, the bitmaps on the PictureBox image is permanent. So, it doesn't matter if you resize, shadows... the PictureBox or the form. The image will be intact. The file is a "ready-to-run" example. A nice function to get the "permanent" graphics object.
Everybody please, reply this message. If this issue is fixed we all are going to win. No more inefficient graphics methods on the Paint event.
Just imagine that you create a program like MS Paint. You draw the curves that the user make with his mouse moves. Just try to repaint it on the Paint event........ god!, you'll need to store ALL the curves in somehow (Paths, Curves).
The answer to this topic, is what we ALL need.
|
|

07-22-2004, 10:52 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
I haven't looked at your project, but if you are doing something like Paint where you are worried about the picture contents being deleted, then you need to use a Bitmap or Image object and use a Graphics object to draw on it so that, when you need to see what you have drawn, you draw the bitmap to the form, and you won't have to store the curves and paths. 
|
|

07-22-2004, 07:05 PM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
What's the problem?
|
Hi, I'm not worried about the picture contents being deleted. The program like MS Paint was just an example. The problem in this thread is:
The drawing process result is not visible until the control is refreshed.
It is visible if you draw on the same procedure that creates the graphics object.
Really, try the code attached previously. You will see this thread in a different way.
|
|

07-22-2004, 07:34 PM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
Dreams come true
About the quote: "It is visible (instantly) if you draw on the same procedure that creates the graphics object." You will say: "Boy, just draw on the same procedure then!". Well, actually, there are too many situations in wich you really need to draw on different functions. Imagine this: You did a game in wich the user move a circle over the control with the keyboard arrows. You may do something like this:
Code:
Dim G As Graphics
Sub MoveCircle(keyCode As Integer)
Select keyCode
Case Left
'Move circle position to left
Case Right
'...
End Select
G = CreatePermanentGraphics(PictureBox1)
G.Clear(PictureBox1.BackColor)
G.DrawEllipse(...) 'Draws the updated position
G.Dispose()
End Sub
Yes!, you're right. Create the object each time the user press a key turns the program very slowly. Actually, I've tested somethink like this, and the "circle" appears to be without any movement until you release the arrow key. It doesn't draw too fast this way. I must say: It draws very slowly this way. Now, imagine it's not a circle but a human figure made of too many shapes...
So, if we fix this issue, we will may do things like this:
Code:
Dim G As Graphics
Sub CreatePermanentsGraphicsForMyPB()
G = CreatePermanentGraphics(PictureBox1)
End Sub
Sub MoveCircle(keyCode As Integer)
Select keyCode
Case Left
'Move circle position to left
Case Right
'...
End Select
G.Clear(PictureBox1.BackColor)
G.DrawEllipse(...) 'Draws the updated position
End Sub
This is nice. You create the graphics object once and draw whenever you want. You may put "CreatePermanentsGraphicsForMyPB" procedure (for example) in the form Load event. even more, you may delete the statement: "G.Clear(PictureBox1.BackColor)" with a simple modification to the function "CreatePermanentGraphics()". But, we have to solve the big problem first.
Run the project attached in order to know what I'm talking about.
|
|

07-24-2004, 05:47 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
I fixed your problem by having the Graphics object come from the PictureBox and not the Image inside the picturebox.
As to why having the graphics come directly from the image, I haven't figured that out yet, but it looks like a screen refresh does something to the Graphics object that's aimed at the picture.
G = PicBox.CreateGraphics()

|
|

07-24-2004, 09:11 PM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
It's not permanent that way
|
Hi Iceplug,
I really appreciate your help, but... we need permanent graphics.
PictureBox1.CreateGraphics() works but the graphics are gone even when a fly pass over the form. That's the normal way, right?
Looks like nobody is interested in having really and powerful permanent graphics.
I'm sure that we all need this.
Greets to all.
|
|

07-25-2004, 06:03 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
Well, maybe if you didn't commit the actual image to the picturebox, but instead, just draw the image onto the picturebox then it'll work for you. 
|
Last edited by Iceplug; 07-26-2004 at 06:18 AM.
|

07-25-2004, 11:32 PM
|
 |
Contributor
|
|
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
|
|
For permanent and powerful graphics, try DirectX. 
|
|

07-26-2004, 08:33 AM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
Finished
I guess "powerful" was a little exaggerated word.
Thanks for your help guys.
|
__________________
"Not to be loved is a simple misfortune; the real misfortune is not to love."
- Albert Camus
|

08-05-2004, 12:07 AM
|
|
Newcomer
|
|
Join Date: Aug 2004
Posts: 1
|
|
May Not be powerful graphics but they are useful
I wanted permanent graphics...I know the problem you meant. Thanx! Your code worked very well for what I wanted! 
|
|

08-05-2004, 08:04 PM
|
|
Newcomer
|
|
Join Date: Jul 2004
Posts: 7
|
|
Of course it's useful!!
|
Of course it's a very useful method!!!!!!.
It's a gold mine. Don't you guys notice that??
Most of people would be very happy with this.
If we refine this method, every one who draw a single line will appreciate it.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|