SetPixel in VB.Net

Epo

Member
Joined
Jul 29, 2003
Messages
7
Hullo there,

Ive been trying to incorporate the SetPixel API into my VB app, but I have to problems:

1) "Inherits System.Drawing.Bitmap.SetPixel" does not work because Intellisense spits out that I can only have one "Inherits" per Class. It also does not appear in my Project->References box to be selected, so, Im just wondering, am I going about it the wrong way? And whats the right way? :)

2) ...I forget...I think just the first one will do, and as soon as I get that figured out, whatever my second question was, will be fixed too :)

Thanks for any insight :)
 
What do you want to SetPixel onto? A Graphics object? A bitmap?

You can SetPixel on a bitmap with the Bitmap.SetPixel method
Code:
Dim bmp As New Bitmap("C:\mybitmap.bmp")

bmp.SetPixel(10, 10, Color.Red) color the pixel at (10, 10) red

If you want to color a pixel on a Graphics object, youll need to draw a small rectangle:
Code:
Dim g As Graphics = Me.CreateGraphics()

g.DrawRectangle(Brushes.Red, 10, 10, 1, 1) Draw a 1x1 rectangle at (10, 10)
 
I am trying to make a fullscreen app that will just display a Colour whenever you click on the Form.

So I guess my graphics object is my main Form...I was hoping to just use GetPixel and SetPixel APIs like in VB6 (those were the days) :) But I guess Its a bit different now...

I tried the "Dim g as Graphics..." line but I get an error saying "Graphics" is a non-defined type...
 
You need to make sure that System.Drawing.dll is referenced and imported in your project.

Also, if you are going to do that, you should put all your code in the forms Paint event. Then, instead of using Me.CreateGraphics(), you use the Graphics objects passed into it. So itd be something like this:
Code:
Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles MyForm.Paint
  Dim g As Graphics = e.Graphics the graphics object of the form is passed in the PaintEventArgs
  
  g.FillRectangle(Brushes.Red, 10, 10, 1, 1)
End Sub
To force the form to redraw, use Me.Invalidate() and the Paint event will be called. For best performance, you should never do any painting outside of the Paint event.
 
Im pretty sure Im referencing it properly. I went into Project->Properties and selected it (double clicked) then clicked on Ok. And System.Drawing is showing up in the References tree at the side....although....Im not quite sure what you mean about Importing it...Im still getting the Invalid type problem...

I was trying to go for simplicity before, but heres what Im really shootin for :) (I was hoping to get it slightly working then move onto this, but it looks like thats just making it more complicated) :)
I have coordinates, X and Y. I have a continuous loop running checking (X,Y) if it is a black pixel or not. If it is a black pixel, I change it to red. The X,Y coordinates are changed by pressing keys on the keyboard. (Is the FormPaint still the way to go?)

Id also like to mention Ive been using .Net for about two days now :) So thanks for being patient :)

Heres my code just so you can see the minimal coding Ive done (and havent) :)
Code:
Public Class Main

    Inherits System.Windows.Forms.Form    

    Private Structure Player
        Dim X As Integer
        Dim Y As Integer
        Dim R As Integer
        Dim V As Integer
    End Structure

    Dim P As Player()
    Dim bRunning As Boolean = True
    Dim G As Graphics = Me.CreateGraphics() 

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = Windows.Forms.FormWindowState.Maximized
        P(1).X = 100
        P(1).Y = 100
        P(1).V = 0
        P(1).R = 0
        While bRunning

        End While
        End
    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Windows.Forms.Keys.Escape
                bRunning = False
        End Select
    End Sub
End Class
(Lines with marks have errors)
 
Maybe this would be a better question...how would I go about colouring in individual pixels at a time?

Thanks again :)
 
This draws a red rectangle on the form
Code:
Private Sub Form_Paint(sender as Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g as Graphics = e.Graphics

g.FillRectangle(Brushes.Red, 10, 10, 50, 50)
End Sub
This is the way drawing is usually done as the drawing is only performed when its needed preventing the computer from slowing down too much

To color pixels:
Code:
Private Sub Form_Paint(sender as Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g as Graphics = e.Graphics

g.FillRectangle(Brushes.Red, 10, 10, 1, 1) Draws five different colored pixels
g.FillRectangle(Brushes.Blue, 15, 15, 1, 1) on the form
g.FillRectangle(Brushes.Green, 20, 20, 1, 1)
g.FillRectangle(Brushes.Orange, 25, 25, 1, 1)
g.FillRectangle(Brushes.Yellow, 30, 30, 1, 1)
End Sub

To draw a picture that the user moves with the keyboard:
Code:
Dim PlayerLoc As Point
Dim PlayerPic as Bitmap = New Bitmap("Player.jpg")

Private Sub Form_Paint(sender as Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g as Graphics = e.Graphics

g.DrawImage(PlayerPic, PlayerLoc.X, PlayerLoc.Y)
End Sub

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Windows.Forms.Keys.Escape
                Me.Close()
            Case Windows.Forms.Keys.Left
                PlayerLoc.X -= 10
                Me.Invalidate() Makes the form redraw when it changes
            Case Windows.Forms.Keys.Right
                PlayerLoc.X += 10
                Me.Invalidate()
            Case Windows.Forms.Keys.Up
                PlayerLoc.Y -= 10
                Me.Invalidate()
            Case Windows.Forms.Keys.Down
                PlayerLoc.Y += 10
                Me.Invalidate()
        End Select
    End Sub
 

Similar threads

C
Replies
0
Views
156
CodingKnight
C
A
Replies
0
Views
135
Alastair MacFarlane (UK)
A
L
Replies
0
Views
149
Les2011
L
Back
Top