ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!! ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
Go Back  Xtreme Visual Basic Talk > > > ALMOST have permanent Graphics!!


Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2004, 06:21 PM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Unhappy 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!)
Reply With Quote
  #2  
Old 07-22-2004, 08:28 AM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Talking 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.
Attached Files
File Type: zip PermanentGraphics.zip (5.3 KB, 35 views)
Reply With Quote
  #3  
Old 07-22-2004, 10:52 AM
Iceplug's Avatar
IceplugALMOST have permanent Graphics!! Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

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.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #4  
Old 07-22-2004, 07:05 PM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Exclamation 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.
Reply With Quote
  #5  
Old 07-22-2004, 07:34 PM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Lightbulb 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.
Reply With Quote
  #6  
Old 07-24-2004, 05:47 AM
Iceplug's Avatar
IceplugALMOST have permanent Graphics!! Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

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()
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #7  
Old 07-24-2004, 09:11 PM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Thumbs down 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.
Reply With Quote
  #8  
Old 07-25-2004, 06:03 AM
Iceplug's Avatar
IceplugALMOST have permanent Graphics!! Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

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.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!

Last edited by Iceplug; 07-26-2004 at 06:18 AM.
Reply With Quote
  #9  
Old 07-25-2004, 11:32 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

For permanent and powerful graphics, try DirectX.
Reply With Quote
  #10  
Old 07-26-2004, 08:33 AM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Cool 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
Reply With Quote
  #11  
Old 08-05-2004, 12:07 AM
EcuadorianAlex EcuadorianAlex is offline
Newcomer
 
Join Date: Aug 2004
Posts: 1
Thumbs up 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!
Reply With Quote
  #12  
Old 08-05-2004, 08:04 PM
Ricardo-007 Ricardo-007 is offline
Newcomer
 
Join Date: Jul 2004
Posts: 7
Smile 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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!! ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!! ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
 
ALMOST have permanent Graphics!!
ALMOST have permanent Graphics!!
 
-->