Advertisement
Guest User

Basic Pixel Operations in vb.net

a guest
Sep 24th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.42 KB | None | 0 0
  1. ''Author: Andrew Buckau
  2. ''Date: Sep. 23, 2013
  3.  
  4. '''WinAPi Class File'''
  5. Public Class WinApi
  6. #Region "API Declarations"
  7.     'Public Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rect) As Boolean
  8.     Public Declare Function GetClientRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As Rect) As Boolean
  9.     Public Declare Function GetDesktopWindow Lib "user32" () As IntPtr
  10.     Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
  11.     Public Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
  12.     Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
  13.     Public Declare Function ClientToScreen Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpPoint As Point) As Boolean
  14.     'Public Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
  15.  
  16. #End Region
  17. #Region "Structs"
  18.     Public Structure Rect
  19.         ''NOT compatible with System.Drawing.Rectangle. Items are declared differently | different order.
  20.         Dim Left As Int32
  21.         Dim Top As Int32
  22.         Dim Right As Int32
  23.         Dim Bottom As Int32
  24.         Public ReadOnly Property Width As Int32
  25.             Get
  26.                 Return Right - Left
  27.             End Get
  28.         End Property
  29.         Public ReadOnly Property Height As Int32
  30.             Get
  31.                 Return Bottom - Top
  32.             End Get
  33.         End Property
  34.     End Structure
  35. #End Region
  36. End Class
  37.  
  38. ''' END WinAPi File ''''''''''''''''''''''''''''''''
  39. ''''''''''''''''''''''''''''''''''''''''''''''''''''
  40. '''ScreenManager Class FIle'''''''''''''''''''''''''
  41. Public Class ScreenManager
  42.     ''' <summary>
  43.     ''' Gets the color of a single pixel and returns as a Color.
  44.     ''' </summary>
  45.     Public Shared Function GetPixel(ByVal x As Int32, ByVal y As Int32) As Color
  46.         Dim hWnd As IntPtr = WinApi.GetDesktopWindow()
  47.         Dim hDC As IntPtr = WinApi.GetWindowDC(hWnd)
  48.         Dim lColor As Int32 = WinApi.GetPixel(hDC, x, y)
  49.         WinApi.ReleaseDC(hWnd, hDC)
  50.         Return ColorTranslator.FromWin32(lColor) '<--nice converter function : Color struct is BIG :/ 20 something bytes.
  51.     End Function
  52.  
  53.     ''' <summary>
  54.     ''' Takes a screenshot of the screen and returns as a Bitmap.
  55.     ''' </summary>
  56.     ''' <param name="tl">Top-Left point of the region to copy</param>
  57.     ''' <param name="width">Width of the region to copy</param>
  58.     ''' <param name="height">Height of the region to copy</param>
  59.     Public Shared Function GetScreenArea(ByVal tl As Point, ByVal width As Int32, ByVal height As Int32) As Bitmap
  60.         Dim _rtnBmp As New Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
  61.         Dim _gr As Graphics = Graphics.FromImage(_rtnBmp)
  62.         _gr.CopyFromScreen(tl, New Point(0, 0), New System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy)
  63.         _gr.Dispose()
  64.         Return _rtnBmp
  65.     End Function
  66.  
  67.     ''' <summary>
  68.     ''' Returns a WinApi.Rect (left,top,right,bottom) for the given window's client area. Does Not include Form Borders.
  69.     ''' </summary>
  70.     ''' <param name="hWnd">Handle to the window</param>
  71.     Public Shared Function GetWindowClientRect(ByVal hWnd As IntPtr) As WinApi.Rect
  72.         Dim _rtnRect As New WinApi.Rect
  73.         Dim _tl As New Point(0, 0)
  74.         WinApi.GetClientRect(hWnd, _rtnRect) '' only fills  Right and Bottom --> is Width and Height.
  75.         WinApi.ClientToScreen(hWnd, _tl)
  76.         _rtnRect.Top = _tl.Y
  77.         _rtnRect.Left = _tl.X
  78.         _rtnRect.Right += _rtnRect.Left
  79.         _rtnRect.Bottom += _rtnRect.Top
  80.         Return _rtnRect
  81.     End Function
  82.  
  83.     ''' <summary>
  84.     ''' Scans through a bitmap for 1 specific color
  85.     ''' </summary>
  86.     ''' <param name="aBitmap">The bitmap to be searched.</param>
  87.     ''' <param name="searchColor">The color to search for.</param>
  88.     Public Shared Function FindColorLocations(ByRef aBitmap As Bitmap, ByVal searchColor As Color) As Point()
  89.         Dim _rtnPts As New List(Of Point)
  90.         Dim _tmpColor As New Color
  91.         For xx As Int32 = 0 To aBitmap.Width - 1
  92.             For yy As Int32 = 0 To aBitmap.Height - 1
  93.                 _tmpColor = aBitmap.GetPixel(xx, yy)
  94.                 If _tmpColor.R = searchColor.R And _tmpColor.G = searchColor.G And _tmpColor.B = searchColor.B Then
  95.                     _rtnPts.Add(New Point(xx, yy)) ''image's pixel-color matched searchColor. Add x,y to the list.
  96.                 End If
  97.             Next
  98.         Next
  99.         If _rtnPts.Count = 0 Then
  100.             _rtnPts.Add(New Point(-1, -1)) '' FAILURE. No points found.
  101.         End If
  102.         Return _rtnPts.ToArray()
  103.     End Function
  104.  
  105. End Class
  106.  
  107. '''END ScreenManager file'''''
  108. ''''''''''''''''''''''''''''''
  109. ''' Form1 demo of use'''''''''
  110. Public Class Form1
  111.     Private _ss As Bitmap '' Screenshot image
  112.     Private _searchColor As Color = Color.FromArgb(255, 0, 0) '' Search Color. Defaulted to pure red.
  113.  
  114.  
  115.     Private Sub cmdTakeSS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTakeSS.Click
  116.         _ss = ScreenManager.GetScreenArea(New Point(0, 0), Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
  117.         pbxSS.Image = _ss
  118.         pbxSS.Refresh()
  119.     End Sub
  120.  
  121.     Private Sub cmdSetSearchColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetSearchColor.Click
  122.         _searchColor = ScreenManager.GetPixel(Cursor.Position.X, Cursor.Position.Y)
  123.         Panel1.BackColor = _searchColor
  124.         Panel1.Refresh()
  125.     End Sub
  126.  
  127.     Private Sub cmdFindColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFindColor.Click
  128.         Dim _foundPoints() As Point = ScreenManager.FindColorLocations(_ss, _searchColor)
  129.  
  130.         If _foundPoints(0).X = -1 Then
  131.             ''fail. todo: add proper documentation. FindColorLocations() returns a single point of (-1,-1) on failure.
  132.             MessageBox.Show("No points found of that color.")
  133.         Else
  134.             Dim _outStr As New System.Text.StringBuilder
  135.  
  136.             For Each pp As Point In _foundPoints
  137.                 _outStr.Append(pp.ToString & "  ") '' displays as {X,Y}
  138.             Next
  139.             MessageBox.Show(_foundPoints.Length & " points found. " & Environment.NewLine _
  140.                         & _outStr.ToString)
  141.         End If
  142.     End Sub
  143. End Class
  144. '''END of Form1 file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement