Solved

Streching Lines in a pictureBox

Posted on 2004-08-18
4
400 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:kouroshparsa
  • 2
4 Comments
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 50 total points
ID: 11840150
Sorry Kouroshparsa, you have to do the redrawing yourself. If you are just drawing circles and lines with the graphics method don't use the picturebox (much to much overhead) use a panel instead.

Something like this, this will draw a simple line and will strech and shrink if you resize the form (the panel is docked)

Dim orwidth As Integer
Dim orheight As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        orwidth = Panel1.Width
        orheight = Panel1.Height
    End Sub

    Dim g As Graphics

    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        g = Panel1.CreateGraphics()
        g.DrawLine(New Pen(Color.Black), 10, 10, CInt(100 * (Panel1.Width / orwidth)), CInt(100 * (Panel1.Height / orheight)))
    End Sub

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

Author Comment

by:kouroshparsa
ID: 11856896
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
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 11859211
One way to handle scaling is to store your objects as percentages of your drawing area instead of absolute coordinates.  This allows you to compute the new object points as you resize the drawing panel or picturebox.

With these changes in mind, this is how your Module and Form would look:

Module Module1

    Public Interface GraphObject
        Sub Paint(ByVal p As Panel, ByRef g As Graphics)
    End Interface

    Public Class LineObject
        Implements GraphObject

        Private pointA As PointF
        Private pointB As PointF

        Private Sub New()
        End Sub

        Public Sub New(ByVal p As Panel, ByVal ptA As PointF, ByVal ptB As PointF)
            pointA = New PointF(ptA.X / p.Width, ptA.Y / p.Height)
            pointB = New PointF(ptB.X / p.Width, ptB.Y / p.Height)
        End Sub

        Public Sub Paint(ByVal p As Panel, ByRef g As Graphics) Implements GraphObject.Paint
            g.DrawLine(Pens.Black, New PointF(pointA.X * p.Width, pointA.Y * p.Height), New PointF(pointB.X * p.Width, pointB.Y * p.Height))
        End Sub
    End Class

    Public Class CircleObject
        Implements GraphObject

        Private pointA As PointF
        Private pointB As PointF

        Private Sub New()
        End Sub

        Public Sub New(ByVal p As Panel, ByVal ptA As PointF, ByVal ptB As PointF)
            pointA = New PointF(ptA.X / p.Width, ptA.Y / p.Height)
            pointB = New PointF((ptB.X - ptA.X) / p.Width, (ptB.Y - ptA.Y) / p.Height)
        End Sub

        Public Sub Paint(ByVal p As Panel, ByRef g As Graphics) Implements GraphObject.Paint
            g.DrawEllipse(Pens.Black, pointA.X * p.Width, pointA.Y * p.Height, pointB.X * p.Width, pointB.Y * p.Height)
        End Sub
    End Class

End Module

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(460, 396)
        Me.Panel1.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(480, 454)
        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(Panel1, New PointF(x1, y1), New PointF(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, 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 c As New CircleObject(Panel1, New PointF(x1, y1), New PointF(x2, y2))
        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(Panel1, e.Graphics)
        Next
    End Sub
End Class
0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 11862513
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I change force the Y axis Maximum in a Visual Studio Chart 5 31
Open a word document 23 33
Definitions and default visual studio colors 5 58
Help with Syntax 9 24
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now