BingWallpaper.psm1

# https://github.com/Jaykul/MultiMonitorHelper
# Requires -Assembly MultiMonitorHelper.dll

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);
'

$urlbase = "http://www.bing.com/"

# A few resolutions of these wallpapers exist, as far as I know ...
$KnownAvailable = [Ordered]@{
  0.56 = @('768x1366','1080x1920')
  0.75 = @('480x640','600x800','768x1024')
  1.33 = @('640x480','800x600','1024x768')
  1.6  = @('1920x1200')
  1.78 = @('1366x768','1920x1080')
}
$Sizes = @($KnownAvailable.Values | % { $_ })
$Ratios = @($knownAvailable.Keys)

function Get-Size {
    param($Size)
    if($Sizes -Contains $Size) {
        return $Size
    }

    [int]$Width, [int]$Height = $Size -split 'x'
    $Ratio = [Math]::Round(($Width / $Height), 2)
    Write-Debug ("{0}x{1} = {2}" -f $Width, $Height, $Ratio)

    $r = [array]::BinarySearch( $Ratios, $Ratio )
    Write-Debug "Index of $Ratio = $r in $Ratios"
    if($r -ge 0) { 
       $Key = $Ratios[$r]
    } else {
        $r = [Math]::Abs($r+2)
        if(($Ratios[$r] - $Ratio) -gt ($Ratio - $Ratios[($r-1)])) {
           $Key = $Ratios[($r-1)]
        } else {
           $Key = $Ratios[$r]
        }
    }

    foreach($Sz in $KnownAvailable[$Key]) {
        $W = [int]($Sz -split 'x')[0]
        Write-Debug ("Can't match {0}x{1}, try {2}" -f $Width, $Height, $W)
        if($W -ge $Width) {
            return $Sz
        }
    }
    return $KnownAvailable[$Key][-1]
}

function Get-ActiveDisplays {
    #.Synopsis
    # Get the currently available displays

    #.Notes
    # Windows.Forms.Screen alters the size of low-DPI monitors in mixed-DPI systems
    # System.Windows.SystemParameters alters the size of hight-DPI monitors in mixed-DPI systems

    # # For example, the following code proved buggy on my systems:
    # [System.Windows.Forms.Screen]::AllScreens | Select DeviceName -Expand Bounds

    # # Given a 3200x1800 high DPI laptop screen and two 1080p screens (one rotated):
    # # It produces this information:

    # Name X Y Width Height
    # ---- - - ----- ------
    # \\.\DISPLAY1 0 0 3200 1800
    # \\.\DISPLAY2 -3840 1448 3840 2160
    # \\.\DISPLAY3 -6000 330 2160 3840

    # # The code below, using https://github.com/ChrisEelmaa/MultiMonitorHelper
    # # Produces the following correct information on my system:
    # Name X Y Height Width Rotation
    # ---- - - ------ ----- --------
    # \\.\DISPLAY1 0 0 1800 3200 Default
    # \\.\DISPLAY2 -1920 724 1080 1920 Default
    # \\.\DISPLAY3 -3000 165 1920 1080 Rotated90

    if(!("MultiMonitorHelper.DisplayFactory" -As [Type])) {
        # This should work on "simple" systems without mixed DPI displays
        [System.Windows.Forms.Screen]::AllScreens | Select DeviceName -Expand Bounds        
    } else {
        @([MultiMonitorHelper.DisplayFactory]::GetDisplayModel().GetActiveDisplays()) |
                    Select Name,
                           @{Name = "X"; Expr = { $_.Origin.X }},
                           @{Name = "Y"; Expr = { $_.Origin.Y }},
                           @{Name = "Height"; Expr = { if($_.Rotation -in "Default", "Rotated180") { $_.Resolution.Height } else { $_.Resolution.Width } }},
                           @{Name = "Width"; Expr = { if($_.Rotation -in "Default", "Rotated180") { $_.Resolution.Width } else { $_.Resolution.Height } }},
                           Rotatione, IsPrimary, IsActive
    }
}

function Set-BingWallpaper {
    #.Synopsis
    # Fetches Bing Homepage images and generates a WallPaper
    #.Description
    # With support for mixed-DPI multi-display configurations, this command will download one or more Bing homepage images from the last several days and generate a custom wallpaper for all your connected screens.
    #.Example
    # Set-BingWallpaper
    # Sets the wallpaper on each of your displays to a different recent Bing homepage image
    #.Example
    # Set-BingWallpaper -Offset 1
    # Sets the wallpaper on each of your displays using the image(s) from yesterday
    #.Notes
    # Uses the CCD APIs which are new in Windows 7 and requires WDDM with display miniport drivers.
    # https://msdn.microsoft.com/en-us/library/windows/hardware/hh406259%28v=vs.85%29.aspx
    [CmdletBinding()]
    param(
        # If you want to try the bing images from other countries, fiddle around with this.
        # As far as I know, the valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA
        # NOTE: as far as I can tell, the images are usually the same, except offset by a day or two in some locales.
        [ValidateSet('en-US', 'zh-CN', 'ja-JP', 'en-AU', 'en-UK', 'de-DE', 'en-NZ', 'en-CA')]
        [String]$Culture,
        # If you want to (re)use yesterday's wallpapers, fiddle around with this
        [Int]$Offset = 0,
        # Force re-downloading and creating the wallpaper even if none of the files have changed
        [Switch]$Force
    )
    begin {
    }
    end {
        # Figure out how many wallpapers we need
        $Screens = Get-ActiveDisplays

        # Use THAT information to calculate our virtual wallpaper size.
        # NOTE: See .Notes in Get-ActiveDisplays as to why we can't use SystemParameters here
        $count = $screens.Count
        $MinX, $MaxX = $Screens | % { $_.X, ($_.X + $_.Width) } | sort | select -first 1 -last 1
        $MinY, $MaxY = $Screens | % { $_.Y, ($_.Y + $_.Height) } | sort | select -first 1 -last 1

        $Width = $MaxX - $MinX
        $Height = $MaxY - $MinY
        $Top = $MinY
        $Left = $MinX

        # 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
        $APIUrl = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=${Offset}&n=${count}"
        if($Culture) {
            $APIUrl += "&mkt=${Culture}"
        }

        $BingImages = Invoke-RestMethod $APIUrl


        $datespan = $Culture + $BingImages.images[-1].startdate + "-" + $BingImages.images[0].enddate

        $TempPath = [System.IO.Path]::GetTempPath()

        $WallPaperPath = Join-Path $TempPath "${datespan}.jpg"

        if(-not $Force -and (Test-Path $WallPaperPath)) {
            Write-Verbose "Update wallpaper from cached image file $WallPaperPath"
        } else {
            $ErrorActionPreference = "Stop"
            Write-Verbose ("Create {0}x{1} Wallpaper from Bing images" -f $Width, $Height)
            try {
                Write-Debug ("Full Wallpaper Size {0}x{1} offset to {2},{3}" -f $Width, $Height, $Left, $Top)
                $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}" -f $Screens[$i].Width, $Screens[$i].Height
                    # Figure out the best image size available ...
                    Write-Debug "Actual Size $Size"
                    $Size = Get-Size $Size
                    Write-Debug "Image Size $Size"

                    # # I wanted to use ProjectOxford to trim the wallpaper, but it's output is grainy
                    # $OCPKey = (BetterCredentials\Get-Credential ComputerVisionApis@api.ProjectOxford.ai -Store).GetNetworkCredential().Password
                    # Invoke-WebRequest -OutFile $WallPaperPath -Method "POST" -ContentType "application/json" `
                    # -Headers @{ "Ocp-Apim-Subscription-Key" = $OCPKey } `
                    # -Body (ConvertTo-Json @{ Url = "http://www.bing.com/iod/1920/1200/{0:yyyyMMdd}" -f (get-date) }) `
                    # -Uri "https://api.projectoxford.ai/vision/v1/thumbnails?width=1024&height=768&smartCropping=true"

                    $File = Join-Path $TempPath ((Split-Path $BingImages.Images[$i].UrlBase -Leaf) + "_" + $Size + ".jpg")

                    if(-not $Force -and (Test-Path $File)){
                        Write-Verbose "Using cached image file $File"
                    } else {
                        $ImageUrl = $urlbase + $BingImages.Images[$i].UrlBase + "_" + $Size + ".jpg"
                        Write-Verbose "Download Image $ImageUrl to $File"
                        Invoke-WebRequest $ImageUrl -OutFile $File
                    }

                    Write-Debug ("Place $(Split-Path $File -Leaf) at {0}, {1} for ({2},{3})" -f ($Screens[$i].X - $Left), ($Screens[$i].Y - $Top), $Screens[$i].Width, $Screens[$i].Height)

                    try {
                        $Source = [System.Drawing.Image]::FromFile($File)
                        # Putting the wallpaper in the right place, relatively speaking, is the tricky bit
                        $Graphics.DrawImage($Source, $Screens[$i].X - $Left, $Screens[$i].Y - $Top, $Screens[$i].Width, $Screens[$i].Height)
                    } finally {
                        $Source.Dispose()
                    }
                }
            } finally {
                $Graphics.Dispose()
                # Save as jpeg to save a little disk space
                $Wallpaper.Save($WallPaperPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
            }
        }
        # Tell windows about our new wallpaper ...
        Set-ItemProperty "HKCU:\Control Panel\Desktop" WallpaperStyle 1
        Set-ItemProperty "HKCU:\Control Panel\Desktop" TileWallpaper 1
        # Please excuse the magic numbers, I can't remember the constants anymore
        [Windows]::SystemParametersInfo( 20, 0, $WallPaperPath, 3 )
    }
}