Results 1 to 4 of 4

Thread: [RESOLVED] Scale Graphics to Image or create custom co-ordinates?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Resolved [RESOLVED] Scale Graphics to Image or create custom co-ordinates?

    Hey,

    I am currently working on drawing out wire diagrams , I get all the co-ordinates from another program and simply draw them out in a picture box.

    vb.net Code:
    1. Private Sub test(Optional mark As Boolean = False)
    2.  
    3.         Dim Img As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    4.  
    5.         Dim g As Graphics = Graphics.FromImage(Img)
    6.  
    7.         g.Clear(Color.White)
    8.  
    9.         Dim graphiccounter As Integer = 0
    10.      
    11.         Dim result As Object
    12.         'Needed to get co-ordinates
    13.         Dim x() As Object
    14.         Dim y() As Object
    15.         Dim z() As Object
    16.         Dim results As Object
    17.  
    18.         'Will take in x and y values from objects above
    19.         Dim x_loc(0) As Integer
    20.         Dim y_loc(0) As Integer
    21.  
    22.  
    23.          _Paths.Clear()
    24.         namepath.Clear()
    25.  
    26.         For Each row As DataGridViewRow In DataGridView2.Rows
    27.  
    28.             If row.Visible = True Then
    29.                 'sets variables to nothing, needed in order to run certain commands
    30.                 results = Nothing
    31.                 x = Nothing
    32.                 y = Nothing
    33.                 z = Nothing
    34.  
    35.              
    36.  
    37.                 'get the co-ordinates and stores them in x,y,z. results will contain the number of co-ordinates stored.
    38.                 result = cor.GetPath(x, y, z)
    39.                
    40.                 're declares variables based on how many co-ordinates
    41.                 ReDim x_loc(result)
    42.                 ReDim y_loc(result)
    43.  
    44.  
    45.          
    46.                 counter = 1
    47.  
    48.                 _Paths.Add(New GraphicsPath)
    49.                 namepath.Add(New NamedPath)
    50.                 For k = 1 To result - 1
    51.  
    52.                     x_loc(k) = (x(k))
    53.                     y_loc(k) = PictureBox1.Height - (y(k))
    54.                     x_loc(k + 1) = (x(k + 1))
    55.                     y_loc(k + 1) = PictureBox1.Height - (y(k + 1))
    56.                     spacer += 5
    57.  
    58.                    
    59.                         _Paths(graphiccounter).AddLine(x_loc(k), y_loc(k), x_loc(k + 1), y_loc(k + 1))
    60.                         namepath(graphiccounter).Name = row.Cells("Core_Id").Value
    61.                         namepath(graphiccounter).Path = _Paths(graphiccounter)
    62.                         'g.FillEllipse(Brushes.Black, x_loc(k + 1) - 4, y_loc(k + 1) - 4, 8, 8)
    63.  
    64.                         counter = counter + 1
    65.          
    66.  
    67.                 Next
    68.  
    69.  
    70.                 _Pen = New Pen(Brushes.HotPink, 5)
    71.                 g.DrawPath(_Pen, namepath(graphiccounter).Path)
    72.                 graphiccounter += 1
    73.  
    74.  
    75.                 '  Console.WriteLine("New Line")
    76.             End If
    77.         Next
    78.      
    79.         PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
    80.         PictureBox1.Image = Img
    81.  
    82.     End Sub

    So that is my code, some of the code may look funky, but that is because I am using a COM object and Windows Script Host Model.

    The results I get back in x and y and co-ordinates based on the other program's co-ordinates. Ofcourse it is easy for me to adjust the co-ordinates so that it works on my screen but how would I go about drawing it so that the image automatically redraws to size based on the window?

    Is it possible to set the co-ordinate system of the GDI graphics? For example a setting to tell windows that every 10 points is just 1.

    so for the picture box of height 100x100. The co-ordinates start at 0 and go to 400, making each point in the picturebox actually 0.25.

    I am looking around for answer but any suggestions is appreciated

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Scale Graphics to Image or create custom co-ordinates?

    you can use the graphics object's scaletransform method:

    Name:  07-05-2013 15.27.10.jpg
Views: 121
Size:  6.0 KB

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SetClientSizeCore(100, 100)
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.ScaleTransform(0.25, 0.25)
            e.Graphics.DrawLine(Pens.Red, 50, 50, 75, 350)
        End Sub
    
    End Class

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Scale Graphics to Image or create custom co-ordinates?

    or if you want the y axis to start at 0 in the bottom left corner:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SetClientSizeCore(100, 100)
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.TranslateTransform(0, 100)
            e.Graphics.ScaleTransform(0.25, -0.25)
            e.Graphics.DrawLine(Pens.Red, 50, 50, 75, 350)
        End Sub
    
    End Class

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Scale Graphics to Image or create custom co-ordinates?

    Quote Originally Posted by .paul. View Post
    you can use the graphics object's scaletransform method:

    Name:  07-05-2013 15.27.10.jpg
Views: 121
Size:  6.0 KB

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SetClientSizeCore(100, 100)
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.ScaleTransform(0.25, 0.25)
            e.Graphics.DrawLine(Pens.Red, 50, 50, 75, 350)
        End Sub
    
    End Class
    Quote Originally Posted by .paul. View Post
    or if you want the y axis to start at 0 in the bottom left corner:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SetClientSizeCore(100, 100)
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            e.Graphics.TranslateTransform(0, 100)
            e.Graphics.ScaleTransform(0.25, -0.25)
            e.Graphics.DrawLine(Pens.Red, 50, 50, 75, 350)
        End Sub
    
    End Class

    Thank You and Thank You

    Exactly what I Was looking for


    EDIT: This is completely throwing of my graphic paths though :\
    Last edited by Crzyrio; May 8th, 2013 at 11:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width