Jump to content

Copy image file based on screen resolution.


Recommended Posts

Posted

Hi,

 

I would like a script that will copy a image file based on the screen resolution. What i am trying to do it set it as the W7 logon wallpaper. The issue is the wallpaper will looked stretched on different screens around if i use a single file. So i am adapting this script - http://www.edugeek.net/forums/windows/85685-desktop-background-issue.html#post754637

 

I have tweaked it and my code looks like this. Can anyone see any errors please?

 

Thanks

 

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
Set objSh = CreateObject("Wscript.Shell")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0) 
For Each objItem in colItems 
   intHorizontal = objItem.ScreenWidth 
   intVertical = objItem.ScreenHeight 
Next

strResolution = CStr(intHorizontal) & " x " & CStr(intVertical)

Select Case strResolution
Case "1024 x 768"
       strWallpaperPath = "\\bishopsgarth\dfs$\Public$\WALLPAPERS\1024x768.bmp"
Case "1280 x 1024"
       strWallpaperPath = "\\bishopsgarth\dfs$\Public$\WALLPAPERS\1280x1024.bmp"
Case "1440 x 900"
       strWallpaperPath = "\\bishopsgarth\dfs$\Public$\WALLPAPERS\1440x900.bmp"
Case "1920 x 1080"
       strWallpaperPath = "\\bishopsgarth\dfs$\Public$\WALLPAPERS\1920x1080.bmp"
Case Else
End Select


filesys.CopyFile strWallpaperPath, "c:\Windows\System32\Oobe\info\backgrounds"

  • 1 year later...
Posted
Can i bump this please :)

 

Although i dont need it for the above purpose anymore i now need it for something else.

 

Thanks

 

What error is it giving etc?

 

Steve

Posted

Using the below code I get the attached message. I am running it from an elevated command prompt. Thanks

 

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
Set objSh = CreateObject("Wscript.Shell")

ScreenRes = funcScreenResolution() 
Function funcScreenResolution() 
   Set objIE = CreateObject("InternetExplorer.Application") 
   objIE.Navigate("about:blank") 
   Do Until objIE.readyState = 4: Loop 
       width = objIE.document.ParentWindow.screen.width 
       height = objIE.document.ParentWindow.screen.height 
   objIE.Quit 
   funcScreenResolution = array(width,height) 
End Function 

strResolution = ScreenRes(0) & " x " & ScreenRes(1)

Select Case strResolution
Case "800 x 600"
       strWallpaperPath = "C:\Temp\Source\800x600.jpg"
Case "1024 x 768"
       strWallpaperPath = "C:\Temp\Source\1024x768.jpg"
Case "1152 x 864"
       strWallpaperPath = "C:\Temp\Source\1152x864.jpg"
Case "1280 x 768"
       strWallpaperPath = "C:\Temp\Source\1280x768.jpg"
Case "1280 x 800"
       strWallpaperPath = "C:\Temp\Source\1280x800.jpg"
Case "1280 x 1024"
       strWallpaperPath = "C:\Temp\Source\1280x1024.jpg"
Case "1366 x 768"
       strWallpaperPath = "C:\Temp\Source\1366x768.jpg"
Case "1440 x 900"
       strWallpaperPath = "C:\Temp\Source\1440x900.jpg"
Case "1680 x 1050"
       strWallpaperPath = "C:\Temp\Source\1680x1050.jpg"
Case "1920 x 1080"
       strWallpaperPath = "C:\Temp\Source\1920x1080.jpg"
Case "1920 x 1440"
       strWallpaperPath = "C:\Temp\Source\1920x1440.jpg"
Case Else
End Select


filesys.CopyFile strWallpaperPath, "C:\Temp\Dest"

copy.PNG

Posted (edited)

That's what it says :) Permissions. Not the script as such.

 

Working here fine (But you'll want to change the last line as you're copy a file into a file without a filetype on, not the folder Dest.

 

Edit - to rephrase you'll want an \ on end of Dest, or make it a proper filename, and try that with permission wise.

 

Steve

Edited by Steve21
  • Thanks 1
Posted
Edit - to rephrase you'll want an \ on end of Dest, or make it a proper filename, and try that with permission wise.

 

That did the trick, thanks

 

Do you think it will work as a startup script please?

Posted
That did the trick, thanks

 

Do you think it will work as a startup script please?

 

Not sure this version will, as you're running IE and getting data without being logged on.

 

Could try it I guess. :( Sorry, not much help :D

 

Steve

Posted
I just tried it, it copies the 1024X768 image, this isnt correct. Any ideas how I could get this to work please?

 

The second script isn't getting your screen height/width though, it's getting IE windows. The first one is getting screen size.

 

What is it you're actually after?

 

Steve

Posted (edited)
The second script isn't getting your screen height/width though, it's getting IE windows. The first one is getting screen size.

 

What is it you're actually after?

 

Steve

 

The first one doesn't work at all on Windows 8 but works fine on Windows 7.

 

Its for the Windows 8 lock screen image. You have to point to a specific image, i want it to display the one at the correct resolution and not just stretch a generic one.

 

This script give me the attached error.

 

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
Set objSh = CreateObject("Wscript.Shell")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0) 
For Each objItem in colItems 
   intHorizontal = objItem.ScreenWidth 
   intVertical = objItem.ScreenHeight 
Next

strResolution = CStr(intHorizontal) & " x " & CStr(intVertical)

Select Case strResolution
Case "800 x 600"
       strWallpaperPath = "C:\Temp\Source\800x600.jpg"
Case "1024 x 768"
       strWallpaperPath = "C:\Temp\Source\1024x768.jpg"
Case "1152 x 864"
       strWallpaperPath = "C:\Temp\Source\1152x864.jpg"
Case "1280 x 768"
       strWallpaperPath = "C:\Temp\Source\1280x768.jpg"
Case "1280 x 800"
       strWallpaperPath = "C:\Temp\Source\1280x800.jpg"
Case "1280 x 1024"
       strWallpaperPath = "C:\Temp\Source\1280x1024.jpg"
Case "1366 x 768"
       strWallpaperPath = "C:\Temp\Source\1366x768.jpg"
Case "1440 x 900"
       strWallpaperPath = "C:\Temp\Source\1440x900.jpg"
Case "1680 x 1050"
       strWallpaperPath = "C:\Temp\Source\1680x1050.jpg"
Case "1920 x 1080"
       strWallpaperPath = "C:\Temp\Source\1920x1080.jpg"
Case "1920 x 1440"
       strWallpaperPath = "C:\Temp\Source\1920x1440.jpg"
Case Else
End Select

filesys.CopyFile strWallpaperPath, "C:\Temp\Dest\Lock-Screen-Image.jpg"

MSI.PNG

Edited by FN-GM
Posted
The first one doesn't work at all on Windows 8 but works fine on Windows 7.

 

Its for the Windows 8 lock screen image. You have to point to a specific image, i want it to display the one at the correct resolution and not just stretch a generic one.

 

This script give me the attached error.

 

dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
Set objSh = CreateObject("Wscript.Shell")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0) 
For Each objItem in colItems 
   intHorizontal = objItem.ScreenWidth 
   intVertical = objItem.ScreenHeight 
Next

strResolution = CStr(intHorizontal) & " x " & CStr(intVertical)

Select Case strResolution
Case "800 x 600"
       strWallpaperPath = "C:\Temp\Source\800x600.jpg"
Case "1024 x 768"
       strWallpaperPath = "C:\Temp\Source\1024x768.jpg"
Case "1152 x 864"
       strWallpaperPath = "C:\Temp\Source\1152x864.jpg"
Case "1280 x 768"
       strWallpaperPath = "C:\Temp\Source\1280x768.jpg"
Case "1280 x 800"
       strWallpaperPath = "C:\Temp\Source\1280x800.jpg"
Case "1280 x 1024"
       strWallpaperPath = "C:\Temp\Source\1280x1024.jpg"
Case "1366 x 768"
       strWallpaperPath = "C:\Temp\Source\1366x768.jpg"
Case "1440 x 900"
       strWallpaperPath = "C:\Temp\Source\1440x900.jpg"
Case "1680 x 1050"
       strWallpaperPath = "C:\Temp\Source\1680x1050.jpg"
Case "1920 x 1080"
       strWallpaperPath = "C:\Temp\Source\1920x1080.jpg"
Case "1920 x 1440"
       strWallpaperPath = "C:\Temp\Source\1920x1440.jpg"
Case Else
End Select

filesys.CopyFile strWallpaperPath, "C:\Temp\Dest\Lock-Screen-Image.jpg"

 

I'm guessing it's just returning null as that device ID doesn't exist in Win8. Can't you just remove the deviceID part and try it?

 

("Select * From Win32_DesktopMonitor") etc

 

Steve

  • Thanks 1
Posted (edited)

Here's a way to do it in PowerShell.

 

function Get-ScreenResolution {
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
   $Screens = [system.windows.forms.screen]::AllScreens

   ForEach ($Screen in $Screens) {
 	$DeviceName = $Screen.DeviceName
$Width      = $Screen.Bounds.Width
$Height     = $Screen.Bounds.Height
$IsPrimary  = $Screen.Primary

   $OutputObj = New-Object -TypeName PSobject
   $OutputObj | Add-Member -MemberType NoteProperty -Name DeviceName -Value $DeviceName
   $OutputObj | Add-Member -MemberType NoteProperty -Name Width -Value $Width
   $OutputObj | Add-Member -MemberType NoteProperty -Name Height -Value $Height
   $OutputObj | Add-Member -MemberType NoteProperty -Name IsPrimaryMonitor -Value $IsPrimary
   $OutputObj

   }
}

$Res = Get-ScreenResolution | Where IsPrimaryMonitor -eq True | Select Width,Height

Switch($Res)
{
   {($_.Width -eq  "800" -and $_.Height -eq  "600" )} { $wp =   "800x600.jpg"; break; } # 4:3
   {($_.Width -eq "1024" -and $_.Height -eq  "768" )} { $wp =  "1024x768.jpg"; break; } # 4:3
   {($_.Width -eq "1152" -and $_.Height -eq  "864" )} { $wp =  "1152x864.jpg"; break; } # 4:3
   {($_.Width -eq "1280" -and $_.Height -eq  "960" )} { $wp =  "1280x960.jpg"; break; } # 4:3
   {($_.Width -eq "1400" -and $_.Height -eq "1050" )} { $wp =  "1280x960.jpg"; break; } # 4:3
   {($_.Width -eq "1920" -and $_.Height -eq "1440" )} { $wp = "1920x1440.jpg"; break; } # 4:3
   {($_.Width -eq "1600" -and $_.Height -eq "1200" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1280" -and $_.Height -eq  "768" )} { $wp =  "1280x768.jpg"; break; } # 5:3
   {($_.Width -eq "1280" -and $_.Height -eq "1024" )} { $wp = "1280x1024.jpg"; break; } # 5:4
   {($_.Width -eq "1280" -and $_.Height -eq  "720" )} { $wp =  "1280x720.jpg"; break; } # 16:9
   {($_.Width -eq "1366" -and $_.Height -eq  "768" )} { $wp =  "1366x768.jpg"; break; } # 16:9
   {($_.Width -eq "1920" -and $_.Height -eq "1080" )} { $wp = "1920x1080.jpg"; break; } # 16:9
   {($_.Width -eq "2560" -and $_.Height -eq "1440" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1280" -and $_.Height -eq  "800" )} { $wp =  "1280x800.jpg"; break; } # 16:10
   {($_.Width -eq "1440" -and $_.Height -eq  "900" )} { $wp =  "1440x900.jpg"; break; } # 16:10
   {($_.Width -eq "1680" -and $_.Height -eq "1050" )} { $wp = "1680x1050.jpg"; break; } # 16:10
   {($_.Width -eq "1920" -and $_.Height -eq "1200" )} { $wp = "1920x1200.jpg"; break; } # 16:10
   {($_.Width -eq "2560" -and $_.Height -eq "1600" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1024" -and $_.Height -eq  "600" )} { $wp =  "1024x600.jpg"; break; } # 17:10
   {($_.Width -eq "2560" -and $_.Height -eq "1080" )} { $wp = "2560x1080.jpg"; break; } # 21:9
   default { $wp = $null }
}

if ( $wp -ne $null ) { 
 Copy-Item -Path "${env:SystemDrive}\Temp\$wp" -Destination "${env:SystemDrive}\Temp\Lock-Screen-Image.jpg" -Force -ErrorAction SilentlyContinue
}

 

If you used the highest resolution for a particular aspect ratio, you would only need seven unique wallpapers in the script above despite there being 20 resolutions. i.e. 1600x1200 would be scaled down to 1024x768, 800x600 etc.

Edited by Arthur
Added Aspect Ratios
  • Thanks 1
  • 5 months later...
Posted
Hi @Arthur i have just got back to working at this (BSF stuff took over). So are you saying if we didn't have an exact resolution the script would copy the best match? thanks
Posted
So are you saying if we didn't have an exact resolution the script would copy the best match?

Sort of. What I was thinking you could do is simply use the largest image for each aspect ratio because Windows should scale it down. This means you would only have to make seven wallpapers in total (less if you didn't require the 17:10 and 21:9 aspect ratios). I have modified the script below so that's it's a bit clearer (and corrected the typo in the previous version above).

 

function Get-ScreenResolution {
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
   $Screens = [system.windows.forms.screen]::AllScreens

   ForEach ($Screen in $Screens) {
 	$DeviceName = $Screen.DeviceName
$Width      = $Screen.Bounds.Width
$Height     = $Screen.Bounds.Height
$IsPrimary  = $Screen.Primary

   $OutputObj = New-Object -TypeName PSobject
   $OutputObj | Add-Member -MemberType NoteProperty -Name DeviceName -Value $DeviceName
   $OutputObj | Add-Member -MemberType NoteProperty -Name Width -Value $Width
   $OutputObj | Add-Member -MemberType NoteProperty -Name Height -Value $Height
   $OutputObj | Add-Member -MemberType NoteProperty -Name IsPrimaryMonitor -Value $IsPrimary
   $OutputObj

   }
}

$Res = Get-ScreenResolution | Where IsPrimaryMonitor -eq $TRUE | Select Width,Height

Switch($Res)
{
   {($_.Width -eq  "800" -and $_.Height -eq  "600" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1024" -and $_.Height -eq  "768" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1152" -and $_.Height -eq  "864" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1280" -and $_.Height -eq  "960" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1400" -and $_.Height -eq "1050" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1920" -and $_.Height -eq "1440" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1600" -and $_.Height -eq "1200" )} { $wp = "1600x1200.jpg"; break; } # [color="#FF0000"]4:3[/color]
   {($_.Width -eq "1280" -and $_.Height -eq  "768" )} { $wp =  "1280x768.jpg"; break; } # [color="#FF0000"]5:3[/color]
   {($_.Width -eq "1280" -and $_.Height -eq "1024" )} { $wp = "1280x1024.jpg"; break; } # [color="#FF0000"]5:4[/color]
   {($_.Width -eq "1280" -and $_.Height -eq  "720" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1366" -and $_.Height -eq  "768" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1920" -and $_.Height -eq "1080" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "2560" -and $_.Height -eq "1440" )} { $wp = "2560x1440.jpg"; break; } # [color="#FF0000"]16:9[/color]
   {($_.Width -eq "1280" -and $_.Height -eq  "800" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1440" -and $_.Height -eq  "900" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1680" -and $_.Height -eq "1050" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1920" -and $_.Height -eq "1200" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "2560" -and $_.Height -eq "1600" )} { $wp = "2560x1600.jpg"; break; } # [color="#FF0000"]16:10[/color]
   {($_.Width -eq "1024" -and $_.Height -eq  "600" )} { $wp =  "1024x600.jpg"; break; } # [color="#FF0000"]17:10[/color]
   {($_.Width -eq "2560" -and $_.Height -eq "1080" )} { $wp = "2560x1080.jpg"; break; } # [color="#FF0000"]21:9[/color]
   default { $wp = $null }
}

if ( $wp -ne $null ) { 
 Copy-Item -Path "${env:SystemDrive}\Temp\$wp" -Destination "${env:SystemDrive}\Temp\Lock-Screen-Image.jpg" -Force -ErrorAction SilentlyContinue
}

 

For example, 1600x1200.jpg would work for all monitors with a 4:3 aspect ratio.

Posted
If the screen resolution changes will the wallpaper automatically update?

Like when someone connects a projector to their laptop? Not sure.

  • 3 months later...
Posted
@Arthur just looking at your script again. Just gave it a try. It doesn't copy a file, but it creates a folder called Lock-Screen-Image.jpg. Any thoughts please?
Posted
Any thoughts please?

Is works for me. Which OS and PowerShell version are you using? :confused:

 

Can you try the following script?

 

#Requires –Version 3
$ErrorActionPreference = "SilentlyContinue"

function Get-ScreenResolution {
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
   [void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
   $Screens = [system.windows.forms.screen]::AllScreens

   ForEach ($Screen in $Screens) {
     $DeviceName = $Screen.DeviceName
     $Width      = $Screen.Bounds.Width
     $Height     = $Screen.Bounds.Height
     $IsPrimary  = $Screen.Primary

     $Output = [PSCustomObject] @{
       'DeviceName'       = $DeviceName;
       'Width'            = $Width;
       'Height'           = $Height;
       'IsPrimaryMonitor' = $IsPrimary;
     }
  }
}

$Res = Get-ScreenResolution | Where IsPrimaryMonitor -eq $true | Select Width,Height

Switch($Res)
{
   {($_.Width -eq  "800" -and $_.Height -eq  "600" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1024" -and $_.Height -eq  "768" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1152" -and $_.Height -eq  "864" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1280" -and $_.Height -eq  "960" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1400" -and $_.Height -eq "1050" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1920" -and $_.Height -eq "1440" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1600" -and $_.Height -eq "1200" )} { $wp = "1600x1200.jpg"; break; } # 4:3
   {($_.Width -eq "1280" -and $_.Height -eq  "768" )} { $wp =  "1280x768.jpg"; break; } # 5:3
   {($_.Width -eq "1280" -and $_.Height -eq "1024" )} { $wp = "1280x1024.jpg"; break; } # 5:4
   {($_.Width -eq "1280" -and $_.Height -eq  "720" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1366" -and $_.Height -eq  "768" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1920" -and $_.Height -eq "1080" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "2560" -and $_.Height -eq "1440" )} { $wp = "2560x1440.jpg"; break; } # 16:9
   {($_.Width -eq "1280" -and $_.Height -eq  "800" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1440" -and $_.Height -eq  "900" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1680" -and $_.Height -eq "1050" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1920" -and $_.Height -eq "1200" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "2560" -and $_.Height -eq "1600" )} { $wp = "2560x1600.jpg"; break; } # 16:10
   {($_.Width -eq "1024" -and $_.Height -eq  "600" )} { $wp =  "1024x600.jpg"; break; } # 17:10
   {($_.Width -eq "2560" -and $_.Height -eq "1080" )} { $wp = "2560x1080.jpg"; break; } # 21:9
   default { $wp = $null }
}

if ( $wp -ne $null ) { 
 Get-ChildItem -Path ${env:SystemDrive}\Temp\* -Include $wp | Copy-Item -Destination "${env:SystemDrive}\Temp\Lock-Screen-Image.jpg" -Force -Verbose
}

Posted

Using 8.1 not sure on Powershell version, what comes with 8.1?. Sorry its taken so long, i got tried up with BSF, then decided to wait until 8.1 :)

 

I will give that new script a try.

 

Thanks again

Posted

What's the resolution of the monitor on your test PC? Are you running the script under your account or System? Are the lock screen images stored in a folder on the C:\ drive or the network?

 

When I ran the script, it copied the image named 2560x1440.jpg (in C:\Temp) to Lock-Screen-Image.jpg in the same folder. My PC is also running Windows 8.1 with PS 4.0. :confused:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...