Support Forums
Pixel graber - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: Pixel graber (/showthread.php?tid=20371)



Pixel graber - Phoenix - 07-09-2011

OK so i decided I would make a tutorial on how to make a program that gets the pixel of whatever the mouse pointer is on.

OK so open up Visual Basic and make a windows form application and name it (whatever you want)
Know make the form fairly small beings we will only be displaying the color of the pixel of whatever the mouse pointer is on.

Know that we have the form sized properly go to the toolbox and select timer and then go to the properties window and set enabled to true and set interval to 1.

Know double click on the timer and put this code in:
Code:
Dim BMP As New Drawing.Bitmap(1, 1)
  Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
  GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), _
  New Drawing.Point(0, 0), BMP.Size)
  Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
  Me.BackColor = Pixel
And then run the project and if done correctly whatever you pace your mouse pointer on it should get that pixel color and display it in the form.

Thank you and please comment.



RE: Pixel graber - Relapse - 07-12-2011

This is pretty useful. I might use this for some upcoming projects!


RE: Pixel graber - Phoenix - 07-19-2011

(07-12-2011, 07:30 PM)Relapse Wrote: This is pretty useful. I might use this for some upcoming projects!
Thanks for your feedback glad i could help.


RE: Pixel graber - AceInfinity - 07-19-2011

Pretty basic stuff here, but nice job for the explanation Smile


RE: Pixel graber - Phoenix - 07-20-2011

(07-19-2011, 07:56 PM)Ace Wrote: Pretty basic stuff here, but nice job for the explanation Smile

Ya i know it is pretty basic its a tutorial when i first started to learn VB but i dont mess much with Visual Basic or Visual programming im more in Java and C/C# scripting but thanks for the feedback.



RE: Pixel graber - Drakon - 07-20-2011

Useful for learning. Thanks.


RE: Pixel graber - Phoenix - 07-28-2011

Glad that i could contribute and help someone your welcome.


RE: Pixel graber - Terridax - 07-28-2011

Just for a future reference, whenever you're making a coding tutorial, you shouldn't just say "Use this code, it will work.". You should explain what all the different functions are doing, so people can understand the code ;)

But thanks for sharing. This'd be useful for anyone wanting to make some sort of a bot for a game ;)


RE: Pixel graber - AceInfinity - 07-28-2011

(07-20-2011, 07:13 AM)RP Deliverance Wrote: Ya i know it is pretty basic its a tutorial when i first started to learn VB but i dont mess much with Visual Basic or Visual programming im more in Java and C/C# scripting but thanks for the feedback.

Though you can still program in C, C#, and C++ in Visual. It's all the same
- Here's my interpretation for others -

'This part defined the pixel as a 1x1 dimension
Code:
Dim BMP As New Drawing.Bitmap(1, 1)

'Getting a Bitmap value with the 1x1 dimension defined previously
Code:
Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(BMP)
Note: I wouldn't suggest using System.Drawing in this way, you should have Imported it at the top of the class and did something like:
Code:
Dim GFX As Graphics = Graphics.FromImage(BMP)

'sets the graphics point as the point at the mouse position with the size of BMP (1x1) on the screen
Code:
GFX.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), _
  New Drawing.Point(0, 0), BMP.Size)

'Pixel is now a system.drawing.color which is equal to the pixel at the mouse position (0,0)
Code:
Dim Pixel As Drawing.Color = BMP.GetPixel(0, 0)
Again I wouldn't put part of the namespace in there, instead do something like this assuming you have imported System.Drawing:
Code:
Dim Pixel As Color = BMP.GetPixel(0,0)

'Puts the color of Pixel found from the color at the mouse location to the backcolor of the form, you can define it as the backcolor of a picturebox if you wanted.
Code:
Me.BackColor = Pixel

You've overused the namespace in your code.
here's a full, more tidy version:
Code:
'Imports System.Drawing

Dim BMP As New Bitmap(1, 1)
  Dim GFX As Graphics = Graphics.FromImage(BMP)
  GFX.CopyFromScreen(New Point(MousePosition.X, MousePosition.Y), New Point(0, 0), BMP.Size)
  Dim Pixel As Color = BMP.GetPixel(0, 0)
  Me.BackColor = Pixel



RE: Pixel graber - Phoenix - 08-03-2011

Awesome but im working on a game now so just incase anyone may be interested check the link in my signature.