Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Visual Basic

Color Grabber

Rate me:
Please Sign up or sign in to vote.
2.15/5 (8 votes)
19 Feb 2010CPOL1 min read 26.5K   272   12   1
A utility to catch a color from the desktop
Screenshot - ColorGrabber.jpg

Introduction

This is a utility to capture a color from the desktop.

Background

I needed an HTML color in a web project, but because I only had a bitmap of the page, I didn't know the colors.

Then I remembered I had a utility written in VB6. I decided to transpose it to VB.NET and add the HTML-color-code to the result.

Using the Code

The utility is quite easy to use. Just run the EXE, click the Go button, and move the mouse around.

When the Go-button is clicked, I catch the desktop and display it as an image on a secondary form displayed underneath the main form. The color of the pixel under the hot spot is displayed in the main form window, and is displayed both in RGB and HTML-code.

By clicking when the "right" color is displayed, the HTML-code is put in the clipboard.

Points of Interest

The old VB6-code was much more verbose. The Form.BackgroundImage property reduced the code a lot. This line of code:

VB.NET
Me.BackgroundImage = PerformCapture()

replaces a lot more VB6 code lines.

Since I don't know of an easy way to find which APIs are embedded in the framework, I kept the old API calls.

For example:

VB.NET
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _ 
Private Shared Function BitBlt(ByVal hdcDest As IntPtr, 
    ByVal nXDest As Int32, ByVal nYDest As Int32, ByVal nWidth As Int32, 
    ByVal nHeight As Int32, ByVal hdcSrc As IntPtr, _ ByVal nXSrc As Int32, 
    ByVal nYSrc As Int32, ByVal dwRop As Int32) As Boolean End Function 

Declare Auto Function CreateDC Lib "gdi32" (ByVal lpDriverName As String, 
    _ ByVal lpDeviceName As String, ByVal lpOutput As String, 
    ByVal lpInitData As IntPtr) As IntPtr

This also shows two different ways of declaring an API in VB.NET.

History

  • 1st October, 2007: Initial post
  • 17th February, 2010: Made some code changes and an upgrade to Visual Studio 2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Johnny J.19-Feb-10 23:53
professionalJohnny J.19-Feb-10 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.