Get Hex Color Code

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
Snyper345
VIP - Donator
VIP - Donator
Posts: 471
Joined: Mon Nov 01, 2010 1:53 am

Get Hex Color Code
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
You do not have the required permissions to view the files attached to this post.
Last edited by Snyper345 on Sat Mar 05, 2011 7:18 pm, edited 2 times in total.
Image
Image
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Get Hex Color Code
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.
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Re: Get Hex Color Code
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
Last edited by Zulf on Sat Mar 05, 2011 7:51 pm, edited 2 times in total.
Image
User avatar
Snyper345
VIP - Donator
VIP - Donator
Posts: 471
Joined: Mon Nov 01, 2010 1:53 am

Re: Get Hex Color Code
Snyper345
Thanks Zulf <3
Image
Image
User avatar
Zulf
Serious Programmer
Serious Programmer
Posts: 441
Joined: Fri Jun 11, 2010 7:46 am

Re: Get Hex Color Code
Zulf
<3 Welcome Dr. Sny
Image
5 posts Page 1 of 1
Return to “Tutorials”