Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Color Grabber

0.00/5 (No votes)
19 Feb 2010 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:

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:

<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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here