Link to home
Start Free TrialLog in
Avatar of kouroshparsa
kouroshparsaFlag for Canada

asked on

Streching Lines in a pictureBox

VB.NET challenge:
Hi everyone.
Let's say I've drawn some lines and circles...in a pictureBox (in the paint event)

Now, I need a simple code for: when the user resizes the form (and subsequently the pictureBox) the picture streches with it.
[I do not want to redraw the lines, circles...I want to strech them]
Feel free to comment
SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kouroshparsa

ASKER

Dear Ronald. Thanks for the comment.
You're absolutely right...
By the way Invalidate and refresh and update all work in such a simple sample.
Let's say I use a Panel.
The idea still does not work for me.
My program graphs curves of any mathematical function. In order to make a presistent drawing, I needed to use an interface...(since I could not put the drawing process in the paint event)
I tried putting "Panel1.Invalidate()" in the paint method of my interface, but it did not work.
Here is a simple sample that you may want to see to figure a solution:
'Instead of Dock I've used Anchor which is fine:
==================
Module1:
==================
Module Module1
    Public Interface GraphObject
        Sub Paint(ByRef g As Graphics)
    End Interface

    Public Class LineObject
        Implements GraphObject

        Private pointA As Point
        Private pointB As Point
        'Private Sub New()
        'End Sub
        Public Sub New(ByVal ptA As Point, ByVal ptB As Point)
            pointA = ptA
            pointB = ptB
        End Sub

        Public Sub Paint(ByRef g As Graphics) Implements GraphObject.Paint
            g.DrawLine(Pens.Black, pointA, pointB)
        End Sub
    End Class

    Public Class CircleObject
        Implements GraphObject

        Private c As Point
        Private r As Integer

        'Private Sub New()
        'End Sub

        Public Sub New(ByVal center As Point, ByVal radius As Integer)
            c = center
            r = radius
        End Sub

        Public Sub Paint(ByRef g As System.Drawing.Graphics) Implements GraphObject.Paint
            g.DrawEllipse(Pens.Blue, _
                New Rectangle(c.X - r, c.Y - r, r * 2, r * 2))
        End Sub
    End Class
End Module
===============
Form1:
===============
Public Class Form1
    Inherits System.Windows.Forms.Form

#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

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Add Line"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(96, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Add Circle"
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Location = New System.Drawing.Point(8, 48)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(272, 208)
        Me.Panel1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Peristent Graphics via Classes"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private rnd As New Random(DateTime.Now().Second)
    Private graphObjects As New ArrayList

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x1, y1, x2, y2 As Integer
        x1 = rnd.Next(0, Panel1.Width + 1)
        y1 = rnd.Next(0, Panel1.Height + 1)
        x2 = rnd.Next(0, Panel1.Width + 1)
        y2 = rnd.Next(0, Panel1.Height + 1)

        Dim l As New LineObject(New Point(x1, y1), New Point(x2, y2))
        graphObjects.Add(l)
        Panel1.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim x1, y1, r As Integer
        x1 = rnd.Next(0, Panel1.Width + 1)
        y1 = rnd.Next(0, Panel1.Height + 1)
        r = rnd.Next(0, (Panel1.Width + 1) / 4)

        Dim c As New CircleObject(New Point(x1, y1), r)
        graphObjects.Add(c)
        Panel1.Refresh()
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        Panel1.Invalidate()
    End Sub

    Private Sub Panel1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim go As GraphObject

        For Each go In graphObjects
            go.Paint(e.Graphics)
        Next
    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I increased the point values...
Mercy Idle-Mind.
I was more thinking of using a method to strech a bitmap containing the image of the picture...Which is very slow and risky
Your idea is great. So the properties of the lines (colour and scale) stay in the memory !

I noticed that a panel repaints faster than a picture box or maybe I'm day-dreaming!!!

By the way, putting "Panel1.Invalidate()" in either Form1_Resize  or in Panel1_Resize works similar but theoretically I think that your choice (Form1_Resize) is better.

Just to know your opinion:
I would be happy to know how the paint event remembers the lines. Does it store all the pixels in the memory or does it put the drawing pieces/changes in the memory? I do not even know how many bytes I'm using when using the Paint event...
For example: After drawing a curve if I want to redraw one of my curves with a different color, I simply regraph the curve with a different color which is very fast without needing any code...so I think that the paint event redraws the last attended color to a pixel.
It all works for me after having the main solutions (I mean the interface) from the good friend Idle-Mind