Function Take-ScreenShot {
<#
.SYNOPSIS
Used to take a screenshot of the desktop or the active window.
.DESCRIPTION
Used to take a screenshot of the desktop or the active window and save to an image file if needed.
.PARAMETER screen
Screenshot of the entire screen
.PARAMETER activewindow
Screenshot of the active window
.PARAMETER file
Name of the file to save as. Default is image.bmp
.PARAMETER imagetype
Type of image being saved. Can use JPEG,BMP,PNG. Default is bitmap(bmp)
.PARAMETER print
Sends the screenshot directly to your default printer
.INPUTS
.OUTPUTS
.NOTES
Name: Take-ScreenShot
Author: Boe Prox
DateCreated: 07/25/2010
.EXAMPLE
Take-ScreenShot -activewindow
Takes a screen shot of the active window
.EXAMPLE
Take-ScreenShot -Screen
Takes a screenshot of the entire desktop
.EXAMPLE
Take-ScreenShot -activewindow -file "C:\image.bmp" -imagetype bmp
Takes a screenshot of the active window and saves the file named image.bmp with the image being bitmap
.EXAMPLE
Take-ScreenShot -screen -file "C:\image.png" -imagetype png
Takes a screenshot of the entire desktop and saves the file named image.png with the image being png
.EXAMPLE
Take-ScreenShot -Screen -print
Takes a screenshot of the entire desktop and sends to a printer
.EXAMPLE
Take-ScreenShot -ActiveWindow -print
Takes a screenshot of the active window and sends to a printer
#>
#Requires -Version 2
[cmdletbinding(
SupportsShouldProcess = $True,
DefaultParameterSetName = "screen",
ConfirmImpact = "low"
)]
Param (
[Parameter(
Mandatory = $False,
ParameterSetName = "screen",
ValueFromPipeline = $True)]
[switch]$screen,
[Parameter(
Mandatory = $False,
ParameterSetName = "window",
ValueFromPipeline = $False)]
[switch]$activewindow,
[Parameter(
Mandatory = $False,
ParameterSetName = "",
ValueFromPipeline = $False)]
[string]$file,
[Parameter(
Mandatory = $False,
ParameterSetName = "",
ValueFromPipeline = $False)]
[string]
[ValidateSet("bmp","jpeg","png")]
$imagetype = "bmp",
[Parameter(
Mandatory = $False,
ParameterSetName = "",
ValueFromPipeline = $False)]
[switch]$print )
# C# code
$code = @'
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
namespace ScreenShotDemo
{
/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
/// <summary>
/// Creates an Image object containing a screen shot the active window
/// </summary>
/// <returns></returns>
public Image CaptureActiveWindow()
{
return CaptureWindow( User32.GetForegroundWindow() );
}
/// <summary>
/// Creates an Image object containing a screen shot of the entire desktop
/// </summary>
/// <returns></returns>
public Image CaptureScreen()
{
return CaptureWindow( User32.GetDesktopWindow() );
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param>
/// <returns></returns>
private Image CaptureWindow(IntPtr handle)
{
// get te hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(handle);
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle,ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height);
// select the bitmap object
IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);
// bitblt over
GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);
// restore selection
GDI32.SelectObject(hdcDest,hOld);
// clean up
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle,hdcSrc);
// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);
// free up the Bitmap object
GDI32.DeleteObject(hBitmap);
return img;
}
/// <summary>
/// Captures a screen shot of the active window, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureActiveWindowToFile(string filename, ImageFormat format)
{
Image img = CaptureActiveWindow();
img.Save(filename,format);
}
/// <summary>
/// Captures a screen shot of the entire desktop, and saves it to a file
/// </summary>
/// <param name="filename"></param>
/// <param name="format"></param>
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename,format);
} /// <summary>
/// Helper class containing Gdi32 API functions
/// </summary>
private class GDI32
{ public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hObjectSource,
int nXSrc,int nYSrc,int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);
} /// <summary>
/// Helper class containing User32 API functions
/// </summary>
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
}
}
'@
#User Add-Type to import the code
add-type $code -ReferencedAssemblies 'System.Windows.Forms','System.Drawing'
#Create the object for the Function
$capture = New-Object ScreenShotDemo.ScreenCapture #Take screenshot of the entire screen
If ($Screen) {
Write-Verbose "Taking screenshot of entire desktop"
#Save to a file
If ($file) {
If ($file -eq "") {
$file = "$pwd\image.bmp"
}
Write-Verbose "Creating screen file: $file with imagetype of $imagetype"
$capture.CaptureScreenToFile($file,$imagetype)
}
ElseIf ($print) {
$img = $Capture.CaptureScreen()
$pd = New-Object System.Drawing.Printing.PrintDocument
$pd.Add_PrintPage({$_.Graphics.DrawImage(([System.Drawing.Image]$img), 0, 0)})
$pd.Print()
}
Else {
$capture.CaptureScreen()
}
}
#Take screenshot of the active window
If ($ActiveWindow) {
Write-Verbose "Taking screenshot of the active window"
#Save to a file
If ($file) {
If ($file -eq "") {
$file = "$pwd\image.bmp"
}
Write-Verbose "Creating activewindow file: $file with imagetype of $imagetype"
$capture.CaptureActiveWindowToFile($file,$imagetype)
}
ElseIf ($print) {
$img = $Capture.CaptureActiveWindow()
$pd = New-Object System.Drawing.Printing.PrintDocument
$pd.Add_PrintPage({$_.Graphics.DrawImage(([System.Drawing.Image]$img), 0, 0)})
$pd.Print()
}
Else {
$capture.CaptureActiveWindow()
}
}
}
#定义文件名(不含后缀名)
$datetime = (Get-Date).Tostring("yyyyMMdd")
$lastday = ((Get-Date 0).AddYears((Get-Date).Year - 1).AddMonths((Get-Date).Month).AddDays(-1)).Day
switch($null) {
{(Get-Date).Day -eq $lastday} {$filename = "Monthly backup " + $datetime}
{(Get-Date).DayOfWeek -ne "Friday" -or "Saturday" -or "Sunday" -and (Get-Date).Day -ne $lastday} {$filename = "Daily backup " + $datetime}
{(Get-Date).DayOfWeek -eq "Friday" -and (Get-Date).Day -ne $lastday} {$filename = "Weekly backup " + $datetime}
}
#执行截图
Start-Sleep -Seconds 2
Take-ScreenShot -activewindow -imagetype jpeg -file "C:\ScreenShot\$filename.JPEG"

[GE]手动截取当前活动窗口,并且按规则命名(1/2)的更多相关文章

  1. [APAC]手动截取当前活动窗口,并且按规则命名(1/2)

    Function Take-ScreenShot { <# .SYNOPSIS Used to take a screenshot of the desktop or the active wi ...

  2. Ruby操作VBA的注意事项和技巧(1):乱码、获取VBA活动和非活动窗口的名称与路径、文件路径的智能拼接与截取(写入日期)

    1.VBA编辑器复制粘贴出来的代码乱码     解决方法:切换到中文输入模式再复制出来就行了 2.获取VBA活动和非活动窗口的名称与路径 Dim wbpath, filename As String ...

  3. QApplication::alert 如果窗口不是活动窗口,则会向窗口显示一个警告(非常好用,效果就和TeamViewer一样)

    void QApplication::alert(QWidget * widget, int msec = 0)如果窗口不是活动窗口,则会向窗口显示一个警告.警报会显示msec 毫秒.如果毫秒为零,闪 ...

  4. win8.1系统下,点击一个窗口,【当前活动窗口】该窗口无法置顶

    两个或多个窗口同时显示在桌面的时候,点击下一层的窗口,无法置顶显示,无论怎么点击,还是隐藏在原置顶窗口的后面,只能手动把原置顶窗口最小化后,才能看到.例如,A窗口现在置顶,B窗口在A的后面,露出来一部 ...

  5. 活动窗口(Active),焦点窗口(Focus)和前景窗口(Foreground)之间的关系

    活动窗口(Active),焦点窗口(Focus)和前景窗口(Foreground)之间的关系 任何一个时候,我们的Windows桌面上总有一个最前台的窗口,其实说简单的,就是标题栏变成深蓝色的那个窗口 ...

  6. Android中简单活动窗口的切换--Android

    本例实现Android中简单Activity窗口切换:借助intent(意图)对应用操作(这里用按钮监听)等的描述,Android根据描述负责找对应的组件,完成组件的调用来实现活动的切换……案例比较简 ...

  7. C#截取当前活动窗体的图片

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. C#启动计算器并设计算器为活动窗口

    启动计算器,并获取焦点 using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { clas ...

  9. [GE]导入图片至Word,然后按规则命名(2/2)

    #将所有docx文件改成可读 Set-ItemProperty -Path "e:\screenshot\*.docx" -Name IsReadOnly -Value $fals ...

随机推荐

  1. 在Win8中创建热点,共享网络

    在Win8中创建热点,共享网络 办公室中,我独享10M光纤,没什么要下的,便想利用来更新下Ipad里面的程序,下点公开课.那在不利用软件[用很多wifi共享的软件],从win7开始 系统本身就自带相关 ...

  2. fuser命令小结

    前提 linux环境下,当使用umount命令卸载挂载点时,会遇到“device is busy”提示,这时fuser就能查出谁在使用这个资源;当然umount –lf  [挂载点] 也可以强制卸载 ...

  3. ubuntu下常用服务器的构建

    1 ftp 1.1 ftp服务器 1.安装vsftpd服务器 sudo apt-get install vsftpd 2.配置vsftpd.conf文件 sudo vi /etc/vsftpd.con ...

  4. kvm NET 和 BRIDGE

    net: <interface type='network'> <mac address='52:54:00:e1:ac:43'/> <source network='d ...

  5. svn Error: post-commit hook failed (exit code 127) with output

    Command: Commit Modified: C:\Users\xsdff\Desktop\project\index.html Sending content: C:\Users\xsdff\ ...

  6. platform_device与platform_driver

    转自:http://blog.csdn.net/zhandoushi1982/article/details/5130207 做Linux方面也有三个多月了,对代码中的有些结构一直不是很明白,比如pl ...

  7. Jquery的tmpl

    jquery 中的tmpl类似于asp.net中的datalist控件. 首选,在页面代码中加入两行,jquery的js文件引用 <script src="http://code.jq ...

  8. Emacs简易教程

    Emacs简易教程阅读: 命令: $emacs 进入之后,输入: C-h t 这里,C-h表示按住[Ctrl]键的同时按h ####### 20090620 *退出: 输入“C-x C-c” *撤销: ...

  9. select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型, 原型: int select(int maxfd,fd_set *rds ...

  10. 使用 Docker 建立 Mysql 集群

    软件环境介绍操作系统:Ubuntu server 64bit 14.04.1Docker 版本 1.6.2数据库:Mariadb 10.10 (Mariadb 是 MySQL 之父在 MySQL 被 ...