Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hullo there,

 

I've 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, I'm just wondering, am I going about it the wrong way? And what's 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 :)

  • *Experts*
Posted

What do you want to SetPixel onto? A Graphics object? A bitmap?

 

You can SetPixel on a bitmap with the Bitmap.SetPixel method

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, you'll need to draw a small rectangle:

Dim g As Graphics = Me.CreateGraphics()

g.DrawRectangle(Brushes.Red, 10, 10, 1, 1) 'Draw a 1x1 rectangle at (10, 10)

Posted

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 API's like in VB6 (those were the days) :) But I guess It's a bit different now...

 

I tried the "Dim g as Graphics..." line but I get an error saying "Graphics" is a non-defined type...

  • *Experts*
Posted

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 form's Paint event. Then, instead of using Me.CreateGraphics(), you use the Graphics objects passed into it. So it'd be something like this:

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.

Posted

I'm pretty sure I'm 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....I'm not quite sure what you mean about Importing it...I'm still getting the Invalid type problem...

 

I was trying to go for simplicity before, but here's what I'm really shootin for :) (I was hoping to get it slightly working then move onto this, but it looks like that's 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?)

 

I'd also like to mention I've been using .Net for about two days now :) So thanks for being patient :)

 

Here's my code just so you can see the minimal coding I've done (and haven't) :)

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)

Posted

Maybe this would be a better question...how would I go about colouring in individual pixels at a time?

 

Thanks again :)

Posted

This draws a red rectangle on the form

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:

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:

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...