Created
April 28, 2021 17:23
-
-
Save HerXayah/dc31edb97639ac1b7826d122d0a36d76 to your computer and use it in GitHub Desktop.
Changes UserImage of Windows 10 Machine via Script and Local JPG 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
[CmdletBinding(SupportsShouldProcess=$true)]Param() | |
function Test-Null($InputObject) { return !([bool]$InputObject) } | |
Function ResizeImage() { | |
param([String]$ImagePath, [Int]$Quality = 100, [Int]$targetSize, [String]$OutputLocation = 'C:\Users\Public\Pictures\') | |
Add-Type -AssemblyName "System.Drawing" | |
$img = [System.Drawing.Image]::Fromfile((get-item './Input.jpg')) | |
$CanvasWidth = $targetSize | |
$CanvasHeight = $targetSize | |
#Encoder parameter for image quality | |
$ImageEncoder = [System.Drawing.Imaging.Encoder]::Quality | |
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1) | |
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($ImageEncoder, $Quality) | |
# get codec | |
$Codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where {$_.MimeType -eq 'image/jpeg'} | |
#compute the final ratio to use | |
$ratioX = $CanvasWidth / $img.Width; | |
$ratioY = $CanvasHeight / $img.Height; | |
$ratio = $ratioY | |
if ($ratioX -le $ratioY) { | |
$ratio = $ratioX | |
} | |
$newWidth = [int] ($img.Width * $ratio) | |
$newHeight = [int] ($img.Height * $ratio) | |
$bmpResized = New-Object System.Drawing.Bitmap($newWidth, $newHeight) | |
$graph = [System.Drawing.Graphics]::FromImage($bmpResized) | |
$graph.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$graph.Clear([System.Drawing.Color]::White) | |
$graph.DrawImage($img, 0, 0, $newWidth, $newHeight) | |
#save to file | |
$bmpResized.Save($OutputLocation, $Codec, $($encoderParams)) | |
$bmpResized.Dispose() | |
$img.Dispose() | |
} | |
$user_sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value | |
Write-Verbose "Updating account picture for User with Local File..." | |
#set up image sizes and base path | |
$image_sizes = @(32, 40, 48, 96, 192, 200, 240, 448, 1080) | |
$image_mask = "Image{0}.jpg" | |
$image_base = $env:public + "\AccountPictures" | |
#set up registry | |
$reg_base = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\AccountPicture\Users\{0}" | |
$reg_key = [string]::format($reg_base, $user_sid) | |
$reg_value_mask = "Image{0}" | |
If ((Test-Path -Path $reg_key) -eq $false) { New-Item -Path $reg_key } | |
$dir = $image_base + "\" + $user_sid | |
#save images, set reg keys | |
ForEach ($size in $image_sizes) | |
{ | |
#save photo to disk, overwrite existing files | |
$file_name = ([string]::format($image_mask, $size)) | |
$pathtmp = $dir + "\_" + $file_name | |
$path = $dir + "\" + $file_name | |
Write-Verbose " saving: $file_name" | |
$user_photo | Set-Content -Path $pathtmp -Encoding Byte -Force | |
ResizeImage $pathtmp $size $size $path | |
Remove-Item $pathtmp | |
#save the path in registry, overwrite existing entries | |
$name = [string]::format($reg_value_mask, $size) | |
$value = New-ItemProperty -Path $reg_key -Name $name -Value $path -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment