how to keep the drawing on picturebox when new drawing is added?VB.Net

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by MJ » Thu, 18 Dec 2003 04:10:35


i have form1 and form2, form1 has a picturebox which i
already drew something on it(e.g. rectangle)
i active form2 to chose another shape to add below the
rect, after form2 closed, the new shape should be added
below the rect and the rect should be remained in its
original position on the picbox(something like drawing
flowchart).i used picbox_Paint event handler to draw the
rect, but when i added new shape using the same Paint
handler the rect is gone??!is there anyway to fix this
problem?? what i try to find is the way to keep the
drawing on the picbox remained till the application is
closed... below is my current coding summary

private sub picbox_paint(e....) .... handles paint

if drawmode = 1 then
e.graphic.drawrect() 'this is the first drawing
picbox.invalide() 'refresh picbox
end if

if drawmode = 2 then 'this means user chose another shape
from form2
e.graphic.drawXX() 'this is second drawing
picbox.invalide() 'refresh picbox
end if

end sub

any help is greatly appreciated...
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Paul Hethe » Thu, 18 Dec 2003 05:02:48

Hi MJ,
First off, DON'T call picbox.invalidate inside the paint event.
Secondly in your logic the first rect will only be drawn the first time and
never after that
Change ... if drawmode=1 then... TO ...if drawmode>=1 then... this should
help

Finally, Read Bob Powell's GDI+ FAQ www.bobpowell.net it will answer this
and most other questions.

Cheers,
Paul

 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Criru » Thu, 18 Dec 2003 15:54:50

You can double buffer the image... use a bitmap of the Form's size to draw
onto, then just draw whole bitmap onto the Form.. this way you will draw
anything without loosing previous draws

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by MJ » Thu, 18 Dec 2003 16:35:55


thanks for replying me!!
i can understand ur solution but i am a beginner in
VB.net, so how to do a double buffer on the image?
u mean after i draw first image then save it to a bitmap,
after user define second drawing on the picbox, just copy
the bitmap content back to the picbox rite?
can u provide me coding summary about the double buffer??


Form's size to draw
way you will draw
from you


shape
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Criru » Thu, 18 Dec 2003 16:57:07

Well, you just need to declare a bitmap globaly in your form

Public Class Form1
Inherits System.Windows.Forms.Form
Private bufferBmp as Bitmap

'then in your form contructor
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
bufferBmp =new bitmap(Me.Width, Me.Height) ' or wich ever size your
drawing surface is
End Sub
'........

'now yo have a in-memory bitmap you can draw on:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
'Draw buferBMP if needed
DrawBuffer()
e.Graphics.DrawImage(bufferBMP,0,0)
End Sub

Private Sub DrawBuffer()
Dim g as Graphics=Graphics.FromImage(bufferBMP)
'use g to draw on bufferBMP what you need
'code here
g.Dispose() 'Dont ever forget this (memory freeing)
End Sub


--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"MJ" < XXXX@XXXXX.COM > wrote in message
news:071e01c3c470$67ad2e90$ XXXX@XXXXX.COM ...


 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by MJ » Thu, 18 Dec 2003 19:27:01

hanks again for help man!!
ur solution works!!
but i discover minor different with the normal drawing in
picbox, i declared

Dim myfont As Font = New Font(New FontFamily("Arial"), 8,
FontStyle.Regular)

and call

g.DrawString("Start", myfont, mysolidbrush, startdrawx, d)

but when i use the double buffering method, the string
shown in the picbox seems like become bolded and unclear,
this nvr happen when i draw directly on the picbox..
is there any solution to solve the minor problem?
anyway i really appreciate ur help...

form
Designer.
InitializeComponent() call
wich ever size your
As
from you
bitmap,
copy
buffer??
i
the
added
drawing
the
this
is
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Criru » Thu, 18 Dec 2003 20:11:39

ry an overloaded version of Bitmap constructor with a specified PixelFormat
and use Format32bppArgb or Format32bppPArgb.

I usualy draw directly on the form or a control of my own instead PictureBox

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"MJ" < XXXX@XXXXX.COM > wrote in message
news:075601c3c488$4ec49bd0$ XXXX@XXXXX.COM ...


 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by MJ » Thu, 18 Dec 2003 22:49:02

orry for bothering u... i really dunno the coding about
the used bitmap constructor and Format32bppArgb or
Format32bppPArgb. can u show me how to set my bufferbmp to
the kind of format u told me?? thanks again very much!!


specified PixelFormat
instead PictureBox
from you
in
8,
d)
unclear,
your
e
MyBase.Paint
the
this
all
which
its
draw
Paint
the
application
another
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Criru » Thu, 18 Dec 2003 23:03:55

nstead using New Bitmap()...
Use a overloaded version

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"MJ" < XXXX@XXXXX.COM > wrote in message
news:0af201c3c4a4$87861d60$ XXXX@XXXXX.COM ...


 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by MJ » Fri, 19 Dec 2003 00:58:07

rr...sorry man, i really dunno how to call and use the
overloaded version bitmap, can u provide me a clearer
coding plz? thanks for help...


from you
message
to
own
drawing
("Arial"),
startdrawx,
string
Me.Height) ' or
ByVal
freeing)
all
in
just
of
it
below
be
in
fix
paint
drawing
 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Paul Hethe » Fri, 19 Dec 2003 04:36:43

think if you are having problems with function overloading you might want
to brush up on .NET programming in general. Overloaded Constructors are used
ALOT, especially in GDI+


"MJ" < XXXX@XXXXX.COM > wrote in message
news:05de01c3c4b6$901c4690$ XXXX@XXXXX.COM ...


 
 
 

how to keep the drawing on picturebox when new drawing is added?VB.Net

Post by Criru » Fri, 19 Dec 2003 15:25:42

f you have Visual Studio NET, you will get a popup describing each
overloaded version of a function.. If you are confused by hte term itself,
you really should pick up a VB book to see the changes from VB 6.. Is to
hard to explain.

This time I'll give the code.. but dont espect anyone to have this time
everytime :)

myBMP = New Bitmap(Me.Width, Me.Height, PixelFormat.Format32bppPArgb)



--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Paul Hetherington" < XXXX@XXXXX.COM > wrote in
message news: XXXX@XXXXX.COM ...
want
used