December 7, 2022

This is How to Crop an Image & Screenshot using Windows Power Automate Desktop (WPAD)

Windows Power Automate Desktop (WPAD) can be used to do many Image-related operations, including but not limited to taking screenshots. We can write bots that take screenshots and then sends them to us via Email, Whatsapp or Telegram. This can be a monitored website or a portion of the page.

Windows Power Automate Desktop (WPAD) Logo
Windows Power Automate Desktop (WPAD)

While taking screenshots is very easy, there is no direct way to capture a portion of the web page or image with fixed coordinates. Below are some of the steps that I use that work flawlessly and can be imported into your program without any issues.

Here is the logic:

  1. Take a Screenshot and copy it to the clipboard.
  2. Launch Microsoft Paint and paste the screenshot.
  3. Move the mouse to the correct coordinates and select the required portion.
  4. Crop the selected part.
  5. Save the screenshot (Optional)
  6. Copy the screenshot
  7. Send the saved file or paste the cropped screenshot in the email draft or document.
  8. Job Done.

Below is the code for the above logic that can be copy pasted in Windows Power Automate Desktop (WPAD)

Workstation.TakeScreenshot.TakeScreenshot _
WAIT 1
System.RunApplication.RunApplicationAndWaitToLoad ApplicationPath: $'''\"C:\\Windows\\System32\\mspaint.exe\"''' WindowStyle: System.ProcessWindowStyle.Maximized Timeout: 60 ProcessId=> AppProcessId WindowHandle=> WindowHandle
WAIT 1
UIAutomation.FocusWindow.FocusByTitleClass Title: $'''Untitled - Paint''' Class: $'''MSPaintApp'''
ON ERROR REPEAT 2 TIMES WAIT 5
ON ERROR WindowNotFoundError
    SET ErrorVal TO $'''MS Paint - Window wasn\'t found'''
    CALL Error_Logs
ON ERROR FocusWindowError
    SET ErrorVal TO $'''MS Paint - Can\'t focus on window'''
    CALL Error_Logs
END
WAIT 3
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Control}({V})''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
WAIT 3
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Escape}''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
MouseAndKeyboard.MoveMouse X: 315 Y: 305 RelativeTo: MouseAndKeyboard.PositionRelativeTo.Screen MovementStyle: MouseAndKeyboard.MovementStyle.Instant
DISABLE MouseAndKeyboard.MoveMouse X: 91 Y: 189 RelativeTo: MouseAndKeyboard.PositionRelativeTo.Screen MovementStyle: MouseAndKeyboard.MovementStyle.Instant
MouseAndKeyboard.SendMouseClick.Click ClickType: MouseAndKeyboard.MouseClickType.LeftButtonDown MillisecondsDelay: 0
MouseAndKeyboard.MoveMouse X: 893 Y: 510 RelativeTo: MouseAndKeyboard.PositionRelativeTo.Screen MovementStyle: MouseAndKeyboard.MovementStyle.Instant
DISABLE MouseAndKeyboard.MoveMouse X: 678 Y: 407 RelativeTo: MouseAndKeyboard.PositionRelativeTo.Screen MovementStyle: MouseAndKeyboard.MovementStyle.Instant
MouseAndKeyboard.SendMouseClick.Click ClickType: MouseAndKeyboard.MouseClickType.LeftButtonUp MillisecondsDelay: 0
WAIT 3
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Control}({C})''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
WAIT 1
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Control}{Shift}({X})''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
WAIT 1
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Control}({S})''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
WAIT 3
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''\"C:\\Users\\Admin\\OneDrive\\Pictures\\MC\\MC-Portfolio-Summary.png\"''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False
WAIT 3
MouseAndKeyboard.SendKeys.FocusAndSendKeys TextToSend: $'''{Return}''' DelayBetweenKeystrokes: 10 SendTextAsHardwareKeys: False

In the above code at the line where Move Mouse is there after Send Keys (Escape), please go to the start of the coordinate that you want to capture in Microsoft Paint and then press Control + Shift to capture the mouse location.

Repeat the same at the end of the mouse pointer where you can end the screencapture. Write the commend below if you want to see it happen in real action or need assistance.