2

I found this snippet that is working well in a loop for me to take screenshot per minute. I could hash results and compare the two latest screenshots for changes by following this post: (Get-FileHash $fileA).hash -ne (Get-FileHash $fileC).hash inside an If statement.

I was thinking of referring to screenshots in the buffer instead of saving them on H.D.D. I mean I want to keep screenshots in buffer and then compare them while that snippet saves them.

1 Answer 1

0

Not possible with the code you say that you are using.

$File = "E:\temp\myscreenshot.bmp"
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing

# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen

# .. monitor width in pixels..
$Width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width

# .. monitor height in pixels..
$Height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height

# .. Capture points
$Left = 0 # ..monitor starting left pixel..
$Top = 0 # ..monitor starting top pixel, normally zero..

Poor man's debug - using variable squeezing to assign and screen output results Remove the edge parens before delivery

# Create bitmap using the top-left and bottom-right bounds
($bitmap = New-Object System.Drawing.Bitmap $Width, $Height)

# Create Graphics object
($graphic = [System.Drawing.Graphics]::FromImage($bitmap))

# Capture screen
($graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size))

# Save to file
$bitmap.Save($File)

You will note, that nothing physical / file system related, via the above output gives you anything you can grab for your use case.

Of course, you can use the code as it, increment the image file name, do the compare and decide which it delete.

The FileSystem cmdlets (like... Get-FileHash, etc...), means you are working on a serialized file.

Get-File​Hash

Module:Microsoft.PowerShell.Utility

Computes the hash value for a file by using a specified hash algorithm.

Update for OP

Personally, I've never tried, and not ever had a use case where it was ever needed. So, it's a concept, but possible, it's all speculation at this point relative to all I understand. Especially hashing and serializing that after doing whatever you plan to do with it. Filesystem cmdlets will only work on the file system by design.

As for putting the stuff in the buffer. I direct you to Trevor Sullivan's blog post here, where he demos initializing a byte array in PowerShell.

Sometimes you’ll need to initialize a new buffer as an array of bytes. For example, if you want to generate random data using the NextBytes() method on the System.Random class, you need to pass in a byte array buffer for the method to write to. Creating a byte array in PowerShell may not be immediately obvious.

4
  • What about comparing byte arrays in buffer, possible?
    – shal
    Commented Apr 2, 2019 at 15:02
  • See my update for you.
    – postanote
    Commented Apr 3, 2019 at 1:57
  • By Get-StringHash, I think it will be possible to generate a hash from a byte stream as it has been done here: powershellgallery.com/packages/PSLogger/1.4.3/Content/…, I mean the 'ComputeHash' part of the code.
    – shal
    Commented Apr 3, 2019 at 7:26
  • That I have seen, but not for what you are after. Yet, it does not hurt to give that a shot inline with what Trevor shows in his examples. The issue here, with screen shots, is that depending on where you are doing this and the size of the grab, you could end up in host resource exhaustion, thus locking up the system, specifically RAM. So, plan for resource release. The other thing I see here, is the compare for change. Yet, you'd need a discoverable unique value, that you can recall, as each matching hash would overwrite before you can compare.
    – postanote
    Commented Apr 3, 2019 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .