?
Solved

draw a pixel

Posted on 2005-04-25
7
Medium Priority
?
436 Views
Last Modified: 2012-05-05
Hi,
I have a form in which I have a button . When I click on that button , I want to draw a pixel in that form with definite co-ordinate . How can I do that ?
0
Comment
Question by:hahahahahaha
  • 4
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
RealMrTea earned 800 total points
ID: 13862073
Simple example with a blank windows form.

    Private alPoints As New ArrayList

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        alPoints.Add(Cursor.Position)
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = e.Graphics
        Dim p As Point
        For Each p In alPoints
            Dim pc As Point = Me.PointToClient(p)
            g.DrawEllipse(New Pen(Color.Blue), pc.X, pc.Y, 5, 5)
        Next
    End Sub

Store the points that were clicked and then each time the form draws, draw the points.

Later,
Eric
0
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13862128
I should also point out that you could use any shape you want...

A g.FillEllipse(Brushes.Blue, pc.X, pc.Y, 3, 3) Looks a little more like a single point.

Enjoy,
Eric
0
 

Author Comment

by:hahahahahaha
ID: 13862334
Public Class DDA
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'DDA
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 358)
        Me.Name = "DDA"
        Me.Text = "DDA"

    End Sub

#End Region
    Dim xa As Integer
    Dim ya As Integer
    Dim xb As Integer
    Dim yb As Integer
    Dim count As Integer
    Dim x, y As Double
    Dim collection As New collection
    Dim p As New Point

    Private Sub DDA_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If count Mod 2 = 0 Then
            xa = Me.MousePosition.X
            ya = Me.MousePosition.Y
        Else
            xb = Me.MousePosition.X
            yb = Me.MousePosition.Y
            LineDDA(xa, ya, xb, yb)
        End If
        count += 1
    End Sub

    Private Sub DDA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        count = 0
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        For Each p In collection
            e.Graphics.DrawRectangle(New Pen(Color.Blue), p.X, p.Y, 1, 1)
        Next
    End Sub

    Private Sub LineDDA(ByVal xa As Integer, ByVal ya As Integer, ByVal xb As Integer, ByVal yb As Integer)
        Dim dx As Integer
        Dim dy As Integer
        Dim steps As Integer
        Dim k As Integer
        Dim xIner, yIner As Double

        dx = xb - xa
        dy = yb - ya
        x = xa
        y = ya
        If Math.Abs(dx) > Math.Abs(dy) Then
            steps = Math.Abs(dx)
        Else
            steps = Math.Abs(dy)
        End If
        xIner = dx / (steps * 1.0)
        yIner = dy / (steps * 1.0)
        p.X = Math.Round(x)
        p.Y = Math.Round(y)
        collection.Add(p)
        For k = 1 To steps
            x += xIner
            y += yIner
            p.X = Math.Round(x)
            p.Y = Math.Round(y)
            collection.Add(p)
        Next
        Me.Refresh()
    End Sub
End Class


I cant understand why it isn't drawn with correct position . And I want to draw a set of pixels , not a set of Rectangles or Ellipses .
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:RealMrTea
ID: 13862371
In your MouseDown Event you need to get the points in the controls coordinates.  They will always be offset if you do it like that.

Dim pClient As Point = Me.PointToClient(Cursor.Position)
xa = pClient.X
ya = pClient.Y

etc...

You may choice to do this on the OnPaint event so they will always be correct even if the form is moved.

That will fix your offset problem,
Eric
0
 

Author Comment

by:hahahahahaha
ID: 13862445
>> A g.FillEllipse(Brushes.Blue, pc.X, pc.Y, 3, 3) Looks a little more like a single point.

We cant draw a pixel in VB.NET , right ? Your solution just is a trick , not actually a pixel . It's an Ellipse .
0
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13862458
Yes.  There is no draw pixle.

Later,
ERic
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 13869160
your LineDDA procedure is setting values in a Collection (called collection), but then you never actuall draw anything on the form.  Me.Refresh does nothing in that procedure.  You need to use a Graphics context, and draw on that context, using GDI+

Me.Refresh does NOT force an OnPaint event.

try this change to your DDALine procedure:

        xIner = dx / (steps * 1.0)
        yIner = dy / (steps * 1.0)
        p.X = Math.Round(x)
        p.Y = Math.Round(y)
        collection.Add(p)
        For k = 1 To steps
            x += xIner
            y += yIner
            p.X = Math.Round(x)
            p.Y = Math.Round(y)
            collection.Add(p)
        Next
'++++++++++++++   New code here  draws the line, but not in the correct location
        Dim g As Graphics = Me.CreateGraphics()
        Dim mypen As New Pen(Color.Red)
        For i As Integer = 1 To collection.Count - 1
           Dim j As Integer = i + 1
           Dim p1 As Point, p2 As Point
           p1 = collection.Item(i)
           p2 = collection.Item(j)
           g.DrawLine(mypen, p1.X, p1.Y, p2.X, p2.Y)
        Next


this will at least now get something drawn on the screen.  It will be up to you to figfure out what it is supposed to do, from here.

AW
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question