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

plotting a point

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

Comments

  • RelmanRelman 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
  • mikecoonmikecoon Posts: 61Member
    really? well that sucks.
    mikecoon
  • iwilld0itiwilld0it 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 ...

    Add this declare in your form ...

    [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.


  • iwilld0itiwilld0it 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]
Sign In or Register to comment.