Click here to Skip to main content
15,883,901 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.4K   272   12  
A utility to catch a color from the desktop
Option Strict On
Option Explicit On
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class mrCapture

   Private Class GDI32
      <DllImport("GDI32.dll")> _
      Public Shared Function BitBlt(ByVal hdcDest As Integer, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As Integer, _
       ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Boolean
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function CreateCompatibleBitmap(ByVal hdc As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function CreateCompatibleDC(ByVal hdc As Integer) As Integer
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function DeleteDC(ByVal hdc As Integer) As Boolean
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function DeleteObject(ByVal hObject As Integer) As Boolean
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function GetDeviceCaps(ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
      End Function
      <DllImport("GDI32.dll")> _
      Public Shared Function SelectObject(ByVal hdc As Integer, ByVal hgdiobj As Integer) As Integer
      End Function
   End Class

   Private Class User32
      <DllImport("User32.dll")> _
      Public Shared Function GetDesktopWindow() As Integer
      End Function
      <DllImport("User32.dll")> _
      Public Shared Function GetWindowDC(ByVal hWnd As Integer) As Integer
      End Function
      <DllImport("User32.dll")> _
      Public Shared Function ReleaseDC(ByVal hWnd As Integer, ByVal hDC As Integer) As Integer
      End Function
   End Class

   Public Sub CaptureScr(ByVal fileName As String, ByVal imageFormat As ImageFormat)
      Dim hdcSrc As Integer = User32.GetWindowDC(User32.GetDesktopWindow()), hdcDest As Integer = GDI32.CreateCompatibleDC(hdcSrc), hBitmap As Integer = GDI32.CreateCompatibleBitmap(hdcSrc, GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10))
      GDI32.SelectObject(hdcDest, hBitmap)
      GDI32.BitBlt(hdcDest, 0, 0, GDI32.GetDeviceCaps(hdcSrc, 8), GDI32.GetDeviceCaps(hdcSrc, 10), hdcSrc, 0, 0, 13369376)
      SaveImageAs(hBitmap, fileName, imageFormat)

      Cleanup(hBitmap, hdcSrc, hdcDest)

   End Sub

   Private Sub Cleanup(ByVal hBitmap As Integer, ByVal hdcSrc As Integer, ByVal hdcDest As Integer)
      User32.ReleaseDC(User32.GetDesktopWindow(), hdcSrc)
      GDI32.DeleteDC(hdcDest)
      GDI32.DeleteObject(hBitmap)
   End Sub

   Private Sub SaveImageAs(ByVal hBitmap As Integer, ByVal fileName As String, ByVal imageFormat As ImageFormat)
      Dim iImage As New Bitmap(Image.FromHbitmap(New IntPtr(hBitmap)), Image.FromHbitmap(New IntPtr(hBitmap)).Width, Image.FromHbitmap(New IntPtr(hBitmap)).Height)
      iImage.Save(fileName, imageFormat)
   End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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