Wallpaper Changer Command Line Utility

.NET Add comments

Description

WallpaperChanger is a command line utility for changing the wallpaper (desktop background) in Windows. The intent of this program is not to be a standalone wallpaper program (it is too cumbersome to use for that). It is intended to be used as a “helper” utility program for another program or script.

For example, I wrote this program for use with a Java program I was writing to change the desktop wallpaper. I ran into a problem trying to get Java to change the desktop wallpaper. The best way to do it is to use one of the Windows system libraries (dlls). I decided to implement a command line utility program in C# to set a new wallpaper. My Java program executed this external utility program when it set the wallpaper.

This utility works fine with Windows XP, Vista, Windows 7, Windows 8, and Windows 10.

Use

The program takes two arguments: the file (including path) to use as wallpaper, and the style (Tiled, Centered, Stretched, Fit, Fill). Instead of a file, a directory can also be specified in which case a random image from the directory will be set as the wallpaper.

Syntax is: [file|directory] [style] [location]

  [file] is the complete path to the file
  [directory] is the complete path to a directory containing image files
    a random image from the directory will be set as the background
  [style] is an integer (if no style is specified it defaults to Stretched):
    0 for Tiled
    1 for Centered
    2 for Stretched
    3 for Fit (Windows 7 or later)
    4 for Fill (Windows 7 or later)
  [location] is the complete path to a directory for storing the generated file
    defaults to the temp folder which should be fine in most cases

If the style argument is not specified it will default to Stretched.

Optional flags:

  • -h, -help – Display the usage help
  • -r, -remove – Remove the current wallpaper
  • -m, -monitor <index> – Set the image on the specified monitor (0 indexed)

When using the monitor option, the full syntax is:

-m <index> <file|directory> <location>

The style does not need to be specified since it appears to always default to Stretched.  Note this functionality is only available on Windows 8 or higher.

Alternatively a config file can be placed in the same directory as the WallpaperChanger executable. The file should be named ‘config’ without
any file extension. Each line in the file should have the full path to an image and can optionally include the monitor index or the style code to use. If the style is not specified it will default to Stretched.

When setting the monitor index in the config file the format of the line should be: <file> -m <index>

The file type can be any of the standard picture types (png, bmp, jpg, gif, etc.). The program will automatically convert the file to a png/bmp file and place it within the users temp directory. Storing the png/bmp file in the temp directory should be fine in most cases, however if you would prefer to use an alternative directory you can specify it as parameter number 3.  On Windows 8 or higher it will use a png file for better quality.  It seems Windows 7 and lower cannot use a png file directly so a bmp file is used instead.  Note: On Windows 7 (and lower) if you are using extremely high resolution images then you may run into the “artifact” problem after Windows sets the file as the background.  If you notice artifacts then my recommendation is to use a separate program to lower the resolution of the original image or try saving it as a different file type first (e.g. jpg).

When using the config file it should be formatted like this:

C:\wallpaper1.jpg
C:\wallpaper\wallpaper2.jpg 3
D:\wallpaper3.png 2
"D:\wallpapers to use\image1.jpg"
C:\wallpaper4.jpg -m 1
etc.

Full paths to the image files should be used and if the path or filename contains spaces you will need to wrap it in quotes.

The program has a return code – 0 for normal, 1 for error

An error code will be returned if the program had an exception, for example invalid syntax, invalid file name, invalid style type, etc.

Requirements

Because WallpaperChanger is written in C#, it requires the .NET Framework (specifically version 2.0 or later) to be installed. If you do not have the .NET Framework installed, you can get it from Windows Update or from the Microsoft website.

Download

Download the WallpaperChanger here (current version: 1.8)

Source Code

I’ve made the source code available on GitHub at https://github.com/philhansen/WallpaperChanger

I currently build the program using Microsoft Visual Studio 2017 Community edition. It targets the .NET Framework v2.0 as that is all it requires.

What’s New

  • 1.8 – Config file now supports the monitor index flag to set the wallpaper for a specific monitor (Windows 8 or higher)
  • 1.7 – Added ability to set the wallpaper for a specific monitor (Windows 8 or higher)
  • 1.6 – Use png file for Windows 8 or higher, otherwise use bmp file
  • 1.5 – Wallpaper is now converted to png instead of bmp to avoid artifacts
  • 1.4 – Added remove flag and config file support
  • 1.3 – Added two new styles for Windows 7 or later (Fit and Fill)

Disclaimer

This program is free for use and distribution. It is delivered “as-is”. I have tested the program and have used it without problem in my own wallpaper changing program.

The code in the program was based off of the code in a CodeProject article found at:
http://www.codeproject.com/dotnet/SettingWallpaperDotNet.asp

60 Responses to “Wallpaper Changer Command Line Utility”

  1. Dmitry Says:

    Thanks for awesome utility dawg! Been looking for something like that for ages.

  2. ddf Says:

    I cant make it work as scheduled task

  3. phil Says:

    You can set it as a scheduled task in windows, but it is annoying how command line programs pop up a little program window when run from the scheduler.

    The best solution for keeping the window hidden seems to be using a VB script. Check out this page for more info http://gallery.technet.microsoft.com/ScriptCenter/8bbed56f-a7aa-491f-a296-687dd96098a3/

    I put that code into a file with a .vbs extension (this makes it a vb script file which windows can execute). For the main line I used this:

    errReturn = objProcess.Create(“C:\wallpapers\WallpaperChanger.exe c:\wallpapers”, null, objConfig, intProcessID)

    You have to include the command line argument which would work ok if you are using a directory containing wallpapers. When I used the vbs file with Windows task scheduler it remained hidden and did not flash any window on the screen.

  4. Justin Swanson Says:

    Just wanted to stop by and say thanks! I had a Java Program in mind to change wallpapers and I stumbled upon your article. Thanks for sharing your work, as it is going to make my life much easier. 8)

  5. phong Says:

    This is the one that I have been looking for. But one thing…. I personaly set my temp folder on a RAM disk volume so that the temp folder is all cleared at boot time. Could you add an option to locate the converted bmp file to another place, or make an alternative build?

  6. phil Says:

    phong, I have received that request before and finally got around to adding that functionality! You can now specify a third parameter which should be a complete path to a directory for storing the generated bmp file. If you don’t specify that parameter it defaults to the temp folder.

  7. phong Says:

    Sorry for the long delay! I downloaded the latest build today. All works fine! I really appreciate it. I use it with nircmd to hide command prompt window (nircmd.exe exec hide WallpaperChanger.exe …), just for your information.

  8. Bo Says:

    I want to change my desktop background to a solid color (choosing “(None)” at the menu), not an image. This program doesn’t seem to support this.

  9. phil Says:

    Hi Bo,

    I would suggest creating (or finding) a small image file with your solid color of choice. Then use the wallpaper changer to set it as the background. Using ‘stretched’ or ’tiled’ should be fine as I don’t think you will notice any difference since it is a solid color. Hope that helps.

  10. James Kuhn Says:

    Hi,

    It seems like the latest version (1.2) is built against a target framework of .NET 4. Each time I try and run it on a machine with .NET 3.5 I get a “Needs .NET framework 4.0.30319 installed – please contact publisher.” error.

    Is the source code available so that I can make the change or is this something that can be updated quickly.

    Many thanks for this tool btw.

    Regards,
    James Kuhn

  11. phil Says:

    Hi James,

    Thanks for the heads up! Apparently at some point my visual studio project started building against .NET 4 as you pointed out. I have recompiled it against .NET 2 as that is really all this small program needs. Please re-download the program and give it another try. If anyone has problems from this point on let me know and I will keep looking into it.

  12. Xavier Says:

    Thanks, very useful and so easy
    Do you think you can add other styles for Windows 7 (fill 10 , fit 6)

    Thanks in advance

    Xavier

  13. phil Says:

    I’ve just posted an updated version (1.3) which adds the two new styles for Windows 7 or later (Fit and Fill). Thanks for the suggestion Xavier.

  14. kfeina Says:

    Hello,

    It would be nice if was possible to define the name of the tmp file (instead of wallpaper.bmp) generated in the [location]?

    Thanks a lot.

  15. Joe Says:

    Hi,

    I like you script and had a couple of questions. I want to use something like this to make a shortcut/program on the desktop that when clicked/ran will randomly select a bmp from a folder, change the wallpaper, and refresh the screen. This is so that I can put a shiny button on my user’s desktops and have some fun hehehe.

  16. phil Says:

    Hi Joe,

    The program should work fine for that. Just create a shortcut and modify the shortcut parameters to include the folder where you have the bmp files located. Whenever you run the program it will pick a random bmp file from the folder and set it as the desktop.

  17. Joe Says:

    Thank you for the fast reply. One more question. How can I turn this into a gui that has the following buttons: save the user’s wallpaper, allow them to choose the random button, restore user WP, and restore Company WP?

  18. Joe Says:

    This is fantastic!!! How do I get it to hide the command prompt window when I execute it? Is this something you have to do when you compile it or is there a switch for it?

  19. phil Says:

    For hiding the command prompt window you will have to use a third party program that specializes in that. Or try following my comment #3 to create a Visual Basic script (.vbs file) and launch it that way. When I tested it before with the task scheduler it remained hidden.

  20. Allen Says:

    Thanks Phil – no more corporate desktop for me!

  21. BGINFO4X Says:

    I use this program as a default wallpaper changer in BGINFO4X.

    BGINFO4X acts as a wrapper, that can simplify your task.

    Thanks.

  22. phil Says:

    Cool project! I’m glad the command line changer is useful. Thanks for letting me know.

  23. Phloo Says:

    This is brilliant!
    I’m more on Java side, and doing this in Java is not so easy. This saved me tons of research! 🙂
    Thank you very much!! I wish you all the best 🙂

  24. Artoria Says:

    Almost perfect, but need anti-alias for high resolution wallpapers
    just trys with a 2560×1440 wallpaper on 1920×1080 screen and compare the results:
    use Change Wallpaper: https://lh6.googleusercontent.com/-ltyHeNjR1xk/Ue9sYYfdMxI/AAAAAAAAPdg/wEpkeg8YPFM/s0/2013-07-24_13-55-32.jpg
    use Windows buit-in set wallpaper: https://lh3.googleusercontent.com/-yGK5vO2LZPU/Ue9sPzpkMPI/AAAAAAAAPdY/qAlAsgvgAOs/s0/2013-07-24_13-55-01.jpg

  25. phil Says:

    Hmm.. I do see the difference. Thanks for posting those two examples. I’m not sure what could be causing the slight quality loss, but I’ll look into it when I get a chance. Can you tell me what file type the original file is?

  26. Artoria Says:

    PNG, original file can be downloaded from http://konachan.com/post/show/164564

  27. phil Says:

    Ok I downloaded the file and tried using the WallpaperChanger. The desktop background looks fine – just like the original picture and just like your second link. I tried setting it with ‘Stretched’, ‘Fit’, and ‘Fill’. It looks fine every time. The WallpaperChanger created a .bmp file in my temp folder that is 10.8mb large, at 2560×1440.

    My primary monitor is 1920×1080 and my secondary monitor is 1680×1050. It looks fine on both.

    So I don’t know what to tell you. Can you locate the .bmp file it created (in your temp folder, unless you specified a different location) and check how that file looks?

  28. Artoria Says:

    Yes I find it, but it’s 14MB large and I will send both of the images to you:
    https://docs.google.com/file/d/0BwBBSZGqNdKwMGp2TnU5WEtadU0/edit?usp=sharing

  29. phil Says:

    From what I can figure out, this appears to be some strange Windows issue. If you google for “windows wallpaper artifacts” you will find a lot of people complaining about Windows wallpapers looking bad when using large png files (and others).

    In this specific case, my WallpaperChanger creates a large .bmp file that when opened looks correct. However when windows sets it as the desktop background it contains the artifacts. I can’t find any way to work around this (and I still don’t understand exactly why it happens).

    For now my suggestion if you run into this issue is to try re-saving the image as a jpg and possibly even lower the resolution to match your desktop resolution.

    When I downloaded the jpg version of the wallpaper you linked, it works fine. So I suggest using the jpg file instead of the png file.

    To be clear, my program converts the image to a bmp file and then uses the Windows system call to set it as the background. I don’t have any control over that system call and I can’t find any workaround.

  30. Artoria Says:

    Yes now I unmderstand it, thank you very much for the explain.

  31. Andrew Says:

    Cool little program, I’m getting so annoyed at Windows 7 not updating the wallpaper from a Group Policy Preference while the user is logged on.

    I don’t suppose there could be an option that just takes the path to the wallpaper and just sets Windows to use this, without creating a .tmp file?

    Also, I stored the wallpaperchanger.exe in C:\Windows\System32\ but it couldn’t find the .NET when I tried using it, I moved it to C:\Windows and it works in there, useful to note.

    Here’s a strange request… can you make another exe or a switch in this one to force change the Lock Screen Background?

    You turn on the option to always use custom wallpaper by a registry setting, then you create this location and put your custom background, MUST be this name:
    C:\Windows\System32\oobe\info\backgrounds\backgroundDefault.jpg

    You then get a custom background on the Lock/Login Screen. However, if the machine is currently on the login or lock screen when it gets a policy update, it doesn’t change the background (same as the desktop background).

    It is a bit concerning that even though I’ve set the Group Policy to prevent users changing their wallpaper, this executable still allows them to, and since currently Windows 7 does not refresh the wallpaper except at logon, this allows users to login, set a new wallpaper for the rest of the day (until they log off), they can put it as a start up script.

  32. James Kuhn Says:

    Hi,

    Great tool. Just wanting to add a feature request.

    Would be nice is the tool could remove any wallpapers that are being used. Currently, the app will return with an error if the file or directory do not exists. Maybe is an empty string was passed in then the current wallpaper would be removed.

    Cheers,

    James

  33. az Says:

    Greetings,

    Thanks for the great tool.

    It would be nice if it could use a config file and randomly select an image based on path and option. For example;
    C:\Images\pic1.jpg 1 (centered)
    C:\Images\pic2.jpg 1 (centered)
    C:\Images\pic3.jpg 0 (stretched)
    D:\Images\pic1.jpg 0 (stretched)

    Thanks!

  34. netix Says:

    You just saved my day. Thanks for this useful tool!

  35. kfeina Says:

    Hello,

    Can you upload the final code of the utility?

    Thanks a lot.

  36. phil Says:

    @James and @az, thanks for the suggestions. I finally got around to updating this utility. You can now remove the current wallpaper by passing a ‘-r’ or ‘-remove’ flag to the executable. On my Windows 7 machine this leaves the desktop background black.

    You can also specify a config file. This should be a file named ‘config’ (no file extension) located in the same directory as the WallpaperChanger.exe file. Each line in the file should be the full path to an image file. You can optionally specify the style code or it will default to Stretched. Simply run the program as “WallpaperChanger.exe” without any arguments and it will look for the config file automatically. The program can of course work without the config file as it always has.

    @Andrew I won’t be doing anything with lock screens.

  37. az Says:

    Phil, thanks for updating. I tested the new version and new functionality and everything works…

  38. Kfeina Says:

    Hello,

    Could you upload the code? I would be very grateful.

    Thanks.

  39. Michael Says:

    Thanks for providing this tool – just writing to say that it works in Win 8.1 too. Cheers.

  40. phil Says:

    Thanks for letting me know Michael 🙂

  41. Luis Says:

    Great tool. I created a similiar tool for changing the Windows 7 logon wallpaper. It allows you to any image of any size and handles all the conversion and resizing itself.

    You can run it from the command line or launch the GUI. It’s very simple by design. I just wanted something that got the job done. You can find it at:

    http://www.simplygoodcode.com/2012/08/change-windows-7-logon-screen-wallpaper/

  42. phil Says:

    Thanks for posting Luis. If anyone wants to change the Windows logon wallpaper, be sure to check out his utility.

  43. Eric Says:

    I’d tried scripts in vbs and ps prior to this, but they proved highly inconsistent (kinda the norm in Windows 8 it seems). This however seems to work perfectly, thank you so much!

  44. Pintu Das Says:

    It’s an awesome tool. I used this tool as a helper program in my application I created. You can find it on http://www.setwallpaper.net76.net

  45. phil Says:

    I’ve made the source code available on GitHub at https://github.com/philhansen/WallpaperChanger

  46. Esp Says:

    It works wonderfully. Thanks a lot.

  47. phil Says:

    I created a new version (1.5) that will convert the wallpaper to png instead of bmp to hopefully avoid any artifacts. Please report any problems.

  48. phil Says:

    Version 1.5 broke the utility on Windows 7 and lower (they cannot use a png file directly as the background). Version 1.6 fixes this as it uses png for Win 8 or higher and bmp for the rest.

  49. Peter Says:

    I use it in a script but I use 2 monitors and I would be great to have a different wallpaper on each monitor, any idea how I can do that ?

  50. phil Says:

    @Peter, as far as I know there is no easy way to do that using the Windows API (which is what this utility uses).

  51. Fritz Says:

    Very nice tool. I found it after looking at numerous web pages that basically said “It can’t be done”. Thanks for publishing it here.

  52. The Virus Maker (jk) Says:

    Thanks for the .exe now I can scare people by changing their background to something random and make my fake virus more real, thanks 🙂

  53. ManelR Says:

    Hello,

    I would like to know if it is possible to have this utility running without requiring the installation of “old” .NET 3.5 in Windows 10.

    Thanks.

  54. phil Says:

    @ManelR, I think it will work in Windows 10 without needing to install anything. Let me know if you have problems. The program is compiled for .Net 2.0.

  55. DicMan Says:

    Hi!
    thanks for providing the program.
    And it’ll be possible to use the config file in mutiple monitors?
    I got an error when using config file such as
    -m 0 XXXX1
    -m 1 xxxxxx2
    -m 2 xxxxxx3
    -m 3 xxxxxx4

    Config file dosn’t work on mutilple monitors.
    Thanks.

  56. phil Says:

    @DicMan, you’re right, the monitor option is not supported with the config file yet. I will add that support when I get a chance (hopefully this week).

    Edit 1/1/2018: I added the monitor option to the config file in version 1.8. The format is slightly different as the file should be listed first: file -m index

  57. DicMan Says:

    @phil,thanks a lot.
    You are the man!
    Thank you very much again.

  58. imod Says:

    Hello,

    Great utility, thanks for share it.
    I wonder is it possible, to add the option to set also the Lock
    Screen background?

    Regards,

  59. phil Says:

    @imod, good question. I did some quick research and it sounds like setting the lock screen can be different depending on different versions of windows. One of the suggestions I read was to change/set a registry key which I would prefer not to do. It would be better if there was a Windows API call to do it.

  60. imod Says:

    Yes, the registry method is a not so good idea, because then the Lock screen personalization windows is blocked with info “Some settings are managed by your organization” :(. Any way – Thanks for the efford 🙂

Leave a Reply

© Phillip Hansen | Original theme by N.Design Studio
Entries RSS Comments RSS Log in