Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear all
i have a picture box then draw a rectangle in it and make it permanent using a user form
then i add a circle to this rectangle by other user form and make it permanent also
the problem is when i try to modify the location of the circle it draw a new one and do not remove old one
and if i draw a new rectangle the all circles disappears
(i want to modify the circles location and remove old one, i want to modify the rectangle dimensions without remove the existing circles)

my code as follow:

VB
Module Module2
    Public canvas As Bitmap
    Public mygraphics As Graphics
    Public Sub draw_rec(ByVal points(,) As Double)

        Dim canavas_x As Integer = frm_main.pb_preview.Width
        Dim canavas_y As Integer = frm_main.pb_preview.Height / 2
        canvas = New Bitmap(canavas_x, canavas_y)
        mygraphics = Graphics.FromImage(canvas)
        Dim myPen As System.Drawing.Pen
        myPen = New System.Drawing.Pen(System.Drawing.Color.Black)
        myPen.Width = 0.0
        mygraphics.Clear(Color.White)
        mygraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        For k = 0 To UBound(points, 1) - 1
            mygraphics.DrawLine(myPen, Convert.ToSingle(points(k, 0)), Convert.ToSingle(points(k, 1)), Convert.ToSingle(points(k + 1, 0)), Convert.ToSingle(points(k + 1, 1)))
        Next
        frm_main.pb_preview.Image = canvas 'make the image permenant
    End Sub
    Public Sub draw_cir()
        Dim myPen As System.Drawing.Pen
        myPen = New System.Drawing.Pen(System.Drawing.Color.Black)
        myPen.Width = 0.0
        Dim mygraphics1 = Graphics.FromImage(canvas)
        mygraphics1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

        For k = 0 To UBound(cicles, 1) - 1
            x = Convert.ToSingle(cicles(k, 1) - 0.5 * cicles(k, 3))
            y = Convert.ToSingle(cicles(k, 2) - 0.5 * cicles(k, 3))
            mygraphics1.DrawEllipse(myPen, x, y, Convert.ToSingle(cicles(k, 3)), Convert.ToSingle(cicles(k, 3)))
        Next
        frm_main.pb_preview.Image = canvas 'make the image permenant


    End Sub
End Module
Posted

You complicate thing a lot by using PictureBox. It does not help you at all, only adds some hassles and consume extra resource and your development time, giving nothing in return. I'll explain what do to. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

And these answers should explain you how to do graphics rendering with changing images, such as interactive and/or animated:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
any way to change a picture at run time with animation[^],
how to make a file from the content of e.graphics[^].

I hope you got the main ideas: 1) you render graphics keeping its model in memory, 2) you change graphics by changing the model and calling of of the Invalidate methods, 3) you avoid flicker by using double buffering.

—SA
 
Share this answer
 
thanks Sergey, i will (try+learn) this Technic and till you when i had a problem
 
Share this answer
 
Comments
Nelek 22-Dec-13 9:15am    
Please don't post solutions to chat with people asking or answering. The messages are not always sorted by date, so it can be a bit difficult to follow them correctly.
The best option is to use the "Have a question or comment?" (or the tiny "reply" on another comment). Another advantage is, that the person you write to will get a notification, otherwise it could be that he/she doesn't see your additional message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900