Click here to Skip to main content
Click here to Skip to main content

Color Grabber

By , 19 Feb 2010
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michael Rosqvist
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberJohnny J.19-Feb-10 23:53 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 19 Feb 2010
Article Copyright 2007 by Michael Rosqvist
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid