ALMOST have permanent Graphics!!

Ricardo-007
07-20-2004, 06:21 PM
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:


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:


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:


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!)

Ricardo-007
07-22-2004, 08:28 AM
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.

Iceplug
07-22-2004, 10:52 AM
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. :)

Ricardo-007
07-22-2004, 07:05 PM
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.

Ricardo-007
07-22-2004, 07:34 PM
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:

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:

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.

Iceplug
07-24-2004, 05:47 AM
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()
:)

Ricardo-007
07-24-2004, 09:11 PM
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.

Iceplug
07-25-2004, 06:03 AM
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. :)

sgt_pinky
07-25-2004, 11:32 PM
For permanent and powerful graphics, try DirectX. :D

Ricardo-007
07-26-2004, 08:33 AM
I guess "powerful" was a little exaggerated word. :rolleyes:

Thanks for your help guys.

EcuadorianAlex
08-05-2004, 12:07 AM
I wanted permanent graphics...I know the problem you meant. Thanx! Your code worked very well for what I wanted! :D

Ricardo-007
08-05-2004, 08:04 PM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum