Created
January 20, 2022 07:48
-
-
Save gocha/9f9cd53b3b28809b182ea0200315e03d to your computer and use it in GitHub Desktop.
スクリーンショットをファイルに保存するコマンド (Command to save a screenshot to a file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
スクリーンショットをファイルに保存します。 | |
.DESCRIPTION | |
プライマリ ディスプレイ全体のスクリーンショットをファイルに保存します。 | |
高 DPI 環境では、正しい範囲をキャプチャすることができません。 | |
.PARAMETER FilePath | |
スクリーンショットの保存先ファイル名。ファイルの形式は PNG です。 | |
.EXAMPLE | |
PS> .\Out-ScreenImage.ps1 image.png | |
#> | |
[CmdletBinding()] | |
param( | |
[parameter(Position = 0, Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$FilePath | |
) | |
Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop | |
# https://stackoverflow.com/questions/11246068/why-dont-net-objects-in-powershell-use-the-current-directory | |
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath | |
$DirectoryPath = (Split-Path -Parent $FilePath) | |
if ($DirectoryPath -and -not (Test-Path $DirectoryPath -PathType Container)) { | |
throw "$DirectoryPath は存在しないかフォルダではありません。" | |
} | |
$Bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds | |
$Bitmap = New-Object System.Drawing.Bitmap($Bounds.Width, $Bounds.Height, [System.Drawing.Imaging.PixelFormat]::Format32bppArgb) | |
$Graphics = [System.Drawing.Graphics]::FromImage($Bitmap) | |
$Graphics.CopyFromScreen($Bounds.X, $Bounds.Y, 0, 0, $Bounds.Size, [System.Drawing.CopyPixelOperation]::SourceCopy) | |
$Graphics.Dispose() | |
$Bitmap.Save($FilePath, [System.Drawing.Imaging.ImageFormat]::Png) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment