Skip to content

Instantly share code, notes, and snippets.

@BenjaminArmstrong
Created June 16, 2016 20:35
Show Gist options
  • Save BenjaminArmstrong/7da166f6cfaf6c641ad53e1d964b82e5 to your computer and use it in GitHub Desktop.
Save BenjaminArmstrong/7da166f6cfaf6c641ad53e1d964b82e5 to your computer and use it in GitHub Desktop.
How to look for an image on a VM screen
$VMName = "Windows"
# Load System.Drawing
Add-Type -AssemblyName "System.Drawing"
# Load AForge libraries - download from http://www.aforgenet.com/framework/
Add-Type -Path "D:\aForge\Release\AForge.dll"
Add-Type -Path "D:\aForge\Release\AForge.Math.dll"
Add-Type -Path "D:\aForge\Release\AForge.Imaging.dll"
# Number parameter is percentage threshold for acuracy. 0.9 = 90% match
$tm = New-Object Aforge.Imaging.ExhaustiveTemplateMatching(0.98)
# Image to match
$imageToMatch = New-Object System.Drawing.Bitmap "C:\Users\benja\Desktop\PShell.bmp"
$VMMS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$($VMName)'"
# Get the resolution of the screen at the moment
$video = $VMCS.GetRelated("Msvm_VideoHead")
$xResolution = $video.CurrentHorizontalResolution[0]
$yResolution = $video.CurrentVerticalResolution[0]
function getVMScreenBMP {
param
(
$VM,
$x,
$y
)
$VMMS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
# Get screenshot
$image = $VMMS.GetVirtualSystemThumbnailImage($VMCS, $x, $y).ImageData
# Transform into bitmap
$BitMap = New-Object System.Drawing.Bitmap -Args $x,$y,Format16bppRgb565
$Rect = New-Object System.Drawing.Rectangle 0,0,$x,$y
$BmpData = $BitMap.LockBits($Rect,"ReadWrite","Format16bppRgb565")
[System.Runtime.InteropServices.Marshal]::Copy($Image, 0, $BmpData.Scan0, $BmpData.Stride*$BmpData.Height)
$BitMap.UnlockBits($BmpData)
# Convert from 16bppRgb565 to 24bppRgb
$RightFormatImage = New-Object System.Drawing.Bitmap -Args $BitMap.Width,$BitMap.Height,Format24bppRgb
$graphics = [System.Drawing.Graphics]::FromImage($RightFormatImage)
$graphics.DrawImage($BitMap, (New-Object System.Drawing.Rectangle(0, 0, $BitMap.Width, $BitMap.Height)))
# Return converted image format
return $RightFormatImage
}
$VMBitmap = getVMScreenBMP $VMCS $xResolution $yResolution
# List any image matches
$tm.ProcessImage($VMBitmap, $imageToMatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment