Page 1 of 1

Get Hex Color Code

Posted: Sat Mar 05, 2011 7:00 pm
by Snyper345
What This does

All this simply does it where ever the mouse hovers it will display the hex code into a textbox and the color hoved into a picture box

Things needed:
1.timer
2.Picture box
3.textbox

Instructions:
Ok add a timer from the toolbox it will be under the Components dropdown or All Windows Forms
Add this code by either double click timer1 or right clicking it and clicking view code
Code: Select all
' the 1,1 or 1x1 is the pixle so its pretty much spot on feel free to edit the 1,1
Dim a As New Drawing.Bitmap(1, 1)
'importing important stuff :P ... Dim pretty much means declare but its short for Dimension/s
        Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(a)



'following mouse position to pick up color
 b.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), a.Size)

'Drawing to picture box 1
        Dim c As Drawing.Color = a.GetPixel(0, 0)
'reading what the back color is to picture box 1
        PictureBox1.BackColor = c
'writes the color hex code to the textbox
        TextBox1.Text = PictureBox1.BackColor.Name
then enable the timer and make the interval anything below 100

now just add a textbox and a picture box and your done :D

sorry if i didnt explain it right lol

Re: Get Hex Color Code

Posted: Sat Mar 05, 2011 7:14 pm
by GoodGuy17
Nice tutorial, Snyper, but can you show us how to click somewhere on the picturebox to get the hex code AND RGB values? That is what I need.

Re: Get Hex Color Code

Posted: Sat Mar 05, 2011 7:25 pm
by Zulf
C# (Hex values and RGB values):
Code: Select all
        public void getHexFromScreen(int coordx, int coordy, PictureBox pb, TextBox tb, TextBox tb2)
        {
            System.Drawing.Bitmap a = new System.Drawing.Bitmap(coordx, coordy);
            System.Drawing.Graphics b = System.Drawing.Graphics.FromImage(a);
            b.CopyFromScreen(new System.Drawing.Point(Control.MousePosition.X, Control.MousePosition.Y), new System.Drawing.Point(0, 0), a.Size);
            System.Drawing.Color c = a.GetPixel(0, 0);
            pb.BackColor = c;
            tb.Text = pb.BackColor.Name.ToString();
            toRGB(tb.Text);
            tb2.Text = toRGB(tb.Text).ToString();
        }

        public System.Drawing.Color toRGB(string colorHex)
        {
            int red = int.Parse(colorHex.Substring(0, 2), NumberStyles.AllowHexSpecifier);
            int green = int.Parse(colorHex.Substring(2, 2), NumberStyles.AllowHexSpecifier);
            int blue = int.Parse(colorHex.Substring(4, 2), NumberStyles.AllowHexSpecifier);
            return System.Drawing.Color.FromArgb(red, blue, green);
        }
VB.NET (Hex values and RGB values):
Code: Select all
Public Sub getHexFromScreen(coordx As Integer, coordy As Integer, pb As PictureBox, tb As TextBox, tb2 As TextBox)
	Dim a As New System.Drawing.Bitmap(coordx, coordy)
	Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(a)
	b.CopyFromScreen(New System.Drawing.Point(Control.MousePosition.X, Control.MousePosition.Y), New System.Drawing.Point(0, 0), a.Size)
	Dim c As System.Drawing.Color = a.GetPixel(0, 0)
	pb.BackColor = c
	tb.Text = pb.BackColor.Name.ToString()
	toRGB(tb.Text)
	tb2.Text = toRGB(tb.Text).ToString()
End Sub

Public Function toRGB(colorHex As String) As System.Drawing.Color
	Dim red As Integer = Integer.Parse(colorHex.Substring(0, 2), NumberStyles.AllowHexSpecifier)
	Dim green As Integer = Integer.Parse(colorHex.Substring(2, 2), NumberStyles.AllowHexSpecifier)
	Dim blue As Integer = Integer.Parse(colorHex.Substring(4, 2), NumberStyles.AllowHexSpecifier)
	Return System.Drawing.Color.FromArgb(red, blue, green)
End Function
Use this code in a timer as:
Code: Select all
getHexFromScreen(1, 1, pictureBox1, textBox1, textBox2);
Imports:
Code: Select all
using System.Globalization;
Screenshot (Hovering over yellow border on icon):
Image

Download (.DLL):
http://www.mediafire.com/?gzdr4q8ach9i49v

A = Alpha(Transparency, 0 = invisible, 255 = solid)
R = Red
B = Blue
G = Green

Re: Get Hex Color Code

Posted: Sat Mar 05, 2011 7:47 pm
by Snyper345
Thanks Zulf <3

Re: Get Hex Color Code

Posted: Sat Mar 05, 2011 7:49 pm
by Zulf
<3 Welcome Dr. Sny