Drawing Lines in a Picturebox

M

Mike Tuersley

Hi,

I have a series of x, y coordinates that I am trying to draw in a
picturebox. I am able to draw lines okay - the problem is trying to draw the
overall shape to the extents of the picturebox. My "image" comes in way too
small. What am I missing? Below are out takes of my code that should show
what I have so far:

Dim width As Integer = PicBox1.Width
Dim height As Integer = PicBox1.Height
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
' set up the drawing surface to draw in the picturebox
g.PageUnit = GraphicsUnit.Point
g.PageScale = 4
_myPen = New Pen(_myForeColor, 1)

g.DrawLine(_myPen, _
_myContour.Vertices.Item(iCntr).PtX, _
_myContour.Vertices.Item(iCntr).PtY, _
_myContour.Vertices.Item(0).PtX, _
_myContour.Vertices.Item(0).PtY)

PicBox1.Image = bmp


Public Structure Contour
Dim Vertices As List(Of Vertex)
End Structure

Public Structure Vertex
Dim PtX As Single
Dim PtY As Single
Public Sub New(ByVal X As Single, ByVal Y As Single)
Me.PtX = X
Me.PtY = Y
End Sub
End Structure
 
M

Morten Wennevik [C# MVP]

Hi Mike,

That's not easy to say because you aren't showing us how you calculate the
_contour, but most likely this is due to you setting PageUnit to Point and
PageScale to 4, which basically makes the bitmap to appear zoomed in on the
upper left corner.
 
M

Mike Tuersley

Thanks Morten,

The _contour is not calculated - it's a list of x,y values that are kicked
out of a spreadsheet. I'm not sure I follow on the PageUnit and PageScale -
what should they be?

Morten Wennevik said:
Hi Mike,

That's not easy to say because you aren't showing us how you calculate the
_contour, but most likely this is due to you setting PageUnit to Point and
PageScale to 4, which basically makes the bitmap to appear zoomed in on the
upper left corner.

--
Happy Coding!
Morten Wennevik [C# MVP]


Mike Tuersley said:
Hi,

I have a series of x, y coordinates that I am trying to draw in a
picturebox. I am able to draw lines okay - the problem is trying to draw the
overall shape to the extents of the picturebox. My "image" comes in way too
small. What am I missing? Below are out takes of my code that should show
what I have so far:

Dim width As Integer = PicBox1.Width
Dim height As Integer = PicBox1.Height
Dim bmp As New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmp)
' set up the drawing surface to draw in the picturebox
g.PageUnit = GraphicsUnit.Point
g.PageScale = 4
_myPen = New Pen(_myForeColor, 1)

g.DrawLine(_myPen, _
_myContour.Vertices.Item(iCntr).PtX, _
_myContour.Vertices.Item(iCntr).PtY, _
_myContour.Vertices.Item(0).PtX, _
_myContour.Vertices.Item(0).PtY)

PicBox1.Image = bmp


Public Structure Contour
Dim Vertices As List(Of Vertex)
End Structure

Public Structure Vertex
Dim PtX As Single
Dim PtY As Single
Public Sub New(ByVal X As Single, ByVal Y As Single)
Me.PtX = X
Me.PtY = Y
End Sub
End Structure
 
M

Morten Wennevik [C# MVP]

Mike Tuersley said:
Thanks Morten,

The _contour is not calculated - it's a list of x,y values that are kicked
out of a spreadsheet. I'm not sure I follow on the PageUnit and PageScale -
what should they be?

The default values for PageUnit is Pixel, and PageScale is 1.0. If you are
unsure if you need them otherwise I would leave them out altogether.
Changing PageScale up or down will cause any drawing using that graphics
object to be scaled up or down relative to 1. The PageUnit property is used
for drawing on various "surfaces" where one unit might not be equal to a
single pixel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top