Last active
March 12, 2018 23:48
-
-
Save Jaykul/f1fb7f12e177f356628b to your computer and use it in GitHub Desktop.
Set the wallpaper(s) without changing options. Sets a different wallpaper on each monitor! :-)
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()] | |
param( | |
# If you want to try the bing images from other countries, fiddle around with this (try en-GB, for instance) | |
$Culture = 'en-US', | |
# If you want to (re)use yesterday's wallpapers, fiddle around with this | |
$Offset = 0 | |
) | |
Add-Type -Assembly System.Windows.Forms, PresentationFramework | |
Add-Type -Name Windows -Namespace System -MemberDefinition @' | |
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
public static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni); | |
'@ | |
# create a bitmap the right size | |
$Height = [System.Windows.SystemParameters]::VirtualScreenHeight | |
$Width = [System.Windows.SystemParameters]::VirtualScreenWidth | |
# And make a note of the absolute position of the top left corder | |
$Top = [System.Windows.SystemParameters]::VirtualScreenTop | |
$Left = [System.Windows.SystemParameters]::VirtualScreenLeft | |
# Figure out how many wallpapers we need | |
$screens = [System.Windows.Forms.Screen]::AllScreens | |
$count = $screens.Count | |
# Fetch Bing's Image Archive information | |
# It would be fun to tell you about these images, right? | |
# TODO: We should do a notification with the details about the new wallpaper | |
$BingImages = Invoke-RestMethod "http://www.bing.com/HPImageArchive.aspx?format=js&idx=${Offset}&n=${count}&mkt=${Culture}" | |
$urlbase = "http://www.bing.com/" | |
$datespan = $Culture + $BingImages.images[-1].startdate + "-" + $BingImages.images[0].enddate | |
$TempPath = [System.IO.Path]::GetTempPath() | |
$WallPaperPath = Join-Path $TempPath "${datespan}.bmp" | |
if(!(Test-Path $WallPaperPath)) { | |
$ErrorActionPreference = "Stop" | |
Write-Verbose "Create Wallpaper from Bing images" | |
$Wallpaper = New-Object System.Drawing.Bitmap ([int[]]($Width, $Height)) | |
$Graphics = [System.Drawing.Graphics]::FromImage($Wallpaper) | |
for($i = 0; $i -lt $Count; $i++) { | |
$Size = "{0}x{1}.jpg" -f $Screens[$i].Bounds.Width, $Screens[$i].Bounds.Height | |
$File = Join-Path $TempPath ($BingImages.Images[$i].startdate + "_" + $Culture + "_" + $Size) | |
# Download the image (assume they'll make one at my resolution) | |
if(!(Test-Path $File)) { | |
$ImageUrl = $urlbase + $BingImages.Images[$i].UrlBase + "_" + $Size | |
Write-Verbose "Download Image $ImageUrl to $File" | |
Invoke-WebRequest $ImageUrl -OutFile $File | |
} else { | |
Write-Verbose "Using cached image file $File" | |
} | |
Write-Verbose ("Draw $File to {0}, {1}" -f ($Screens[$i].Bounds.X - $Left), ($Screens[$i].Bounds.Y - $Top)) | |
$Source = [System.Drawing.Image]::FromFile($File) | |
# Putting the wallpaper in the right place, relatively speaking, is the tricky bit | |
$Graphics.DrawImage($Source, $Screens[$i].Bounds.X - $Left, $Screens[$i].Bounds.Y - $Top) | |
} | |
$Graphics.Dispose() | |
$Wallpaper.Save($WallPaperPath) | |
} else { | |
Write-Verbose "Update wallpaper from cached image file $WallPaperPath" | |
} | |
[Windows]::SystemParametersInfo( 20, 0, $WallPaperPath, 0x03) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment