Results 1 to 6 of 6

Thread: Compare pixel range on screen to a saved image

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Compare pixel range on screen to a saved image

    Scenario: We stream the BBC news channel through a laptop hooked to a PC in reception for our clients. Annoyingly the stream is interrupted randomly (the line isn't dropping, it's the BBC's problem). Therefore when it's interrupted we get an error screen appearing which we have to go and manually refresh to get the stream back playing.

    My intentions are to build an app which will be run alongside (silently) the stream monitoring intermittently a range of pixels. If the range matches the error screen that I mentioned previously then I'd call "Esc" to exit the full screen and then "F5" to refresh the browser and finally a double click on a specified coordinate to bring it back to full screen.

    Problem: I've been doing some googling and so far have only seen related posts where the comparison is between two saved images. Due to this I expect the answer to be "Take a screenshot every now and then, compare, return result".

    Is there a way to accomplish this without taking screen shots?

  2. #2
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Compare pixel range on screen to a saved image

    There is no easy way that I know of. What I would do is Use the graphics.copyfromscreen and save it to a bitmap. I Use lockbits to get an array of Integers or bytes and compare it with the image that I already have of the error screen. This whole operation shouldn't take very long so it should be ideal.
    Last edited by BlindSniper; Dec 1st, 2011 at 11:57 AM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  3. #3
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Compare pixel range on screen to a saved image

    I did a small experiment on my computer to test how long it would take for what I said. My code grab's a screenshot the size of my screen and gets a byte array of the pixels, It then compares the array with another array of the same size and sets a boolean variable if the pixels don't match. All this under 50 Milliseconds.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    278

    Re: Compare pixel range on screen to a saved image

    Wow, I'm quite surprised by that sort of speed.

    Any chance you could post your code for me to refer to? I'm thinking this would (as expected) be the easiest option.

    I'm actually noticing I'm going to have to compare the screenshot to several images (apparently the "error messages" appear to be inconsistent), at the speed you're suggesting this would make no noticeable difference though.

  5. #5
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: Compare pixel range on screen to a saved image

    I created a Blank Project for this.
    You need a reference to System.Drawing And System.Windows.Forms
    vb.net Code:
    1. Imports System.Drawing
    2. Imports System.Windows.Forms
    3. Imports System.Runtime.InteropServices
    4. Imports System.Diagnostics
    5.  
    6. Module Monitor
    7.  
    8.     Private [Exit] As Boolean
    9.     WithEvents Tmr As Timers.Timer
    10.     Sub Main()
    11.         Tmr = New Timers.Timer(1000)
    12.         Tmr.Start()
    13.         While Not [Exit]
    14.             Threading.Thread.Sleep(100)
    15.         End While
    16.     End Sub
    17.     Dim Reference(2983341) As Byte ' should be populated with the error image data.
    18.  
    19.     Private Sub Tmr_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles Tmr.Elapsed
    20.         Dim st As Stopwatch = Stopwatch.StartNew
    21.         Dim ScreenCap As New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height, Imaging.PixelFormat.Format24bppRgb)
    22.         Using g As Graphics = Graphics.FromImage(ScreenCap)
    23.             g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.WorkingArea.Size)
    24.         End Using
    25.         Dim BMD As Imaging.BitmapData = ScreenCap.LockBits(New Rectangle(0, 0, ScreenCap.Width, ScreenCap.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
    26.         Dim Buffer((ScreenCap.Width * ScreenCap.Height - 1) * 3) As Byte
    27.         Marshal.Copy(BMD.Scan0, Buffer, 0, Buffer.Length)
    28.         ScreenCap.UnlockBits(BMD)
    29.         Dim IsCorrect As Boolean
    30.         For i = 0 To Buffer.Length - 1 Step 3
    31.             If Buffer(i) <> Reference(i) Then
    32.                 IsCorrect = False
    33.                 Exit For
    34.             End If
    35.         Next
    36.         'Test IsCorrect here and act on it.
    37.         st.Stop()
    38.         MsgBox(st.ElapsedMilliseconds.ToString)
    39.     End Sub
    40. End Module

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  6. #6
    New Member
    Join Date
    Mar 2012
    Posts
    1

    Re: Compare pixel range on screen to a saved image

    I am trying to do something very similar but I need to do a screenshot every second and compare it with the last screenshot taken and see if they match.... any code ideas how I would do this?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width