שתף באמצעות


drawing a lines in a picturebox?

Question

Tuesday, January 18, 2011 8:23 PM

hummm...

I've looked and tried.. and for some reason just don't seem to be getting right now.

I have a picturebox that is 250x500,  I want to draw a vertical line from the top to the bottom. 

 

Using gs = pbMap.CreateGraphics 
'Do While x.X < 250
'gs.DrawLine(Pens.Red, x, y)
Dim pt1 As New Point(30, 0) 
Dim pt2 As New Point(30, 410) 
gs.DrawLine(Pens.Red, pt1, pt2) 
'gs.DrawLine(Pens.Red, 0, 0, Convert.ToInt32(Me.pb.Width / 2), Convert.ToInt32(Me.pb.Height))
End Using 

 

 

All replies (7)

Tuesday, January 18, 2011 10:07 PM ✅Answered

This is the result of your last post:

Public Class Form1
  Dim WithEvents PB As New PictureBox
  Dim Ratios As PointF
  Dim Box As New RectangleF(5, 25, 0.5, 4)
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    PB.Size = New Size(250, 500)
    Ratios = New PointF(CSng(PB.Width / 55.5), CSng(PB.Height / 2343))
    PB.BackColor = Color.White
    PB.Parent = Me
    Me.ClientSize = PB.Size
  End Sub
  Private Sub PB_Paint(ByVal sender As Object, ByVal e As Windows.Forms.PaintEventArgs) Handles PB.Paint
    Dim Loc As New PointF(Box.X * Ratios.x, Box.Y * Ratios.y)
    Dim Size As New SizeF(Box.Width * Ratios.X, Box.Height * Ratios.Y)
    e.Graphics.FillRectangle(Brushes.Red, New RectangleF(Loc, Size))
  End Sub
End Class

Look closely!


Tuesday, January 18, 2011 8:31 PM

The syntax for drawline() is:

object.drawline(pen, x1, y1, x2, y2)

x1, y1 is the starting coordinate and x2, y2 is the ending coordinate.  VB will draw a line between those two points.

-Diana


Tuesday, January 18, 2011 8:41 PM

To draw on your screen through a window (for example, a PictureBox), draw in the Paint event using the graphics supplied.


Tuesday, January 18, 2011 8:42 PM

grrr.. thanks... well.. I was doing on form load... and the picturebox has a paint on there.. so i was not seeing it, even though it was drawing.

Another question.. kind of pertaining to this.  My picturebox is 250x500.   I would like that to represent number like 48 inches wide and 1800 ft long.  Given that.. if i wanted to represent a red box that was 500 in feet from the top and 30 inchs from the left. and the box is 2 inchs by 3 inches, how would i be able to calculate something like that to be able to draw it?


Tuesday, January 18, 2011 8:54 PM

A to scale PictureBox 48 X (12 X 1800) would seem more appropirate.  But if you must have 250 X 500, scale by the ratios.


Tuesday, January 18, 2011 8:56 PM

You do not need to do it in Paint, but it will not persist if you do not. Here is how you can draw on a PictureBox whenever you want to....

        Dim gr As Graphics = Graphics.FromImage(PictureBox1.Image)

        Dim pt1 As New Point(30, 0)

        Dim pt2 As New Point(30, 410)

 

        gr.DrawLine(Pens.Red, pt1, pt2)

 

        Dim newWidth As Integer = Convert.ToInt32(gr.DpiX * 2.0)    ' 2.0 inches wide

        Dim newHeight As Integer = Convert.ToInt32(gr.DpiY * 3.0)   ' 3.0 inches tall

 

        Dim newX As Integer = Convert.ToInt32(gr.DpiX * 30.0)

        Dim newY As Integer = Convert.ToInt32((gr.DpiY * 12.0) * 500.0)

 

        PictureBox1.Refresh()

        gr.Dispose()

 

 

EDIT : I put the calculations you requested into the snippet. 

 


Tuesday, January 18, 2011 9:46 PM

all of the values would change with the exception of the 250.x500.. I don't need the 250x500, was just using that to try and represnt what I am trying to draw.

one time i could have 55.5in wide and 2343 ft long and want my redbox 25 ft from the top and 5 inches from the left with a box that would represent 4 ft long x .5 inch wide.