plotting a point - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# plotting a point

Posts: 61Member
What is the command to plot a point on the form?
My book talks about rectangles and lines but not points.
mikecoon

• Posts: 104Member
: What is the command to plot a point on the form?
: My book talks about rectangles and lines but not points.
: mikecoon
:

As far as I know, there isn't any "PutPixel" method in the Graphics object. I'd suggest that you just draw a line of length 1. Basically pass the paremeter to the Graphics object:
something like: Graphics.DrawLine(Pen,x,y,x+1,y)

Relman
• Posts: 61Member
really? well that sucks.
mikecoon
• Posts: 1,134Member
[b][red]This message was edited by iwilld0it at 2004-7-22 8:1:14[/red][/b][hr]
: What is the command to plot a point on the form?
: My book talks about rectangles and lines but not points.
: mikecoon
:

Here is a not so obvious way to do it with pure .NET code ...

"Me" is the form object

[code]
Dim w, h As Integer
w = Me.ClientRectangle.Width
h = Me.ClientRectangle.Height

Dim surface As New Bitmap(w, h)
surface.SetPixel(50, 50, Color.Red)

Me.BackgroundImage = surface
[/code]

Here is a way that involves an API call ...

[code]
Declare Function SetPixel Lib "gdi32" Alias "SetPixel" ( _
ByVal hdc As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal crColor As Integer) As Integer
[/code]

And then you can set your pixel like so ...

[code]
Dim hDC As IntPtr = Me.CreateGraphics.GetHdc()

SetPixel(hDC, 50, 50, ColorTranslator.ToWin32(Color.Red))
[/code]

Either way, it would have been easier for .NET to include a function that was easier to access.

• Posts: 1,134Member
: What is the command to plot a point on the form?
: My book talks about rectangles and lines but not points.
: mikecoon
:

I also seen it suggested this way too which seems the least amount code you would type ....

[code]
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
e.Graphics.FillRectangle(Brushes.Red, 50, 50, 1, 1)
End Sub
[/code]