Results 1 to 2 of 2

Thread: GetPixel screenshot doesn't refresh

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    4

    Cool GetPixel screenshot doesn't refresh

    Hello all...I have a program that analysis pixels by taking a screengrab...

    Code:
    Dim region As New Rectangle(0, 0, 1, 1)
      Dim screenBitmap As New Bitmap(region.Width, region.Height, Imaging.PixelFormat.Format64bppPArgb)
            Dim bitmapGraphics As Graphics
            bitmapGraphics = Graphics.FromImage(screenBitmap)
            Using bitmapGraphics
                bitmapGraphics.CopyFromScreen(resetx, resety, 0, 0, region.Size)
            End Using
            Dim c As Color = screenBitmap.GetPixel(0, 0)
            Dim mouseloc As Point = Cursor.Position
    So i use this in a loop but the problem is the first time it runs it works perfectly but then after it loops the r, g, b code stays the same...I'm assuming the problem is that when it loops its using the same screengrab and not taking a new one thus the pixel stays the same even though its changed on the screen...So how do I make it refresh when the loop restarts and take a new screenshot with the code above every time so it properly reports the current pixel color on the current screen? Let me know if this isn't clear...Thanks!

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: GetPixel screenshot doesn't refresh

    The code isn't complete.
    How do we know whether resetx, resety ever change?
    If they don't change, then you will always be getting the same spot.
    You're also not using "Using" correctly.

    And you say it is in a loop, but what kind of loop. You should probably just be executing code from a timer event, rather than looping.
    For testing, I tried the following in a timer and it works fine, printing out the a,r,g,b values in the Output Window of the IDE.
    Code:
      Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim region As New Rectangle(0, 0, 1, 1)
        Dim mouseloc As Point = Cursor.Position
        Using screenBitmap As New Bitmap(region.Width, region.Height, Imaging.PixelFormat.Format64bppPArgb)
          Using bitmapGraphics As Graphics = Graphics.FromImage(screenBitmap)
            bitmapGraphics.CopyFromScreen(mouseloc.X, mouseloc.Y, 0, 0, region.Size)
          End Using
          Dim c As Color = screenBitmap.GetPixel(0, 0)
          Debug.Print(c.ToString)
        End Using
    
      End Sub
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
      End Sub
    Code:
    'example output in the Output window of the IDE
    Color [A=255, R=45, G=117, B=167]
    Color [A=255, R=45, G=117, B=167]
    Color [A=255, R=232, G=145, B=125]
    Color [A=255, R=253, G=253, B=253]

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