Good day to all, comrades!

As part of the functionality of my application is the change of desktop wallpaper. With the help of Google, I found a popular solution in the C # programming language.

I attach the full class code below.

namespace BingPhotoOfTheDay.etc { static class bing { [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern Int32 SystemParametersInfo( UInt32 action, UInt32 uParam, String vParam, UInt32 winIni); private static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14; private static readonly UInt32 SPIF_UPDATEINIFILE = 0x01; private static readonly UInt32 SPIF_SENDWININICHANGE = 0x02; public static string GetURLToImage() { WebClient client = new WebClient(); client.DownloadFile("http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US", "parse.xml"); XmlDocument xDoc = new XmlDocument(); xDoc.Load("parse.xml"); XmlElement xRoot = xDoc.DocumentElement; string URL = ""; XmlNode node = xDoc.DocumentElement.SelectSingleNode("/images/image/urlBase"); URL = node.InnerText + "_1920x1080.jpg"; return URL; } public static void DownloadImage() { WebClient client = new WebClient(); client.DownloadFile("https://bing.com" + GetURLToImage(), "image.jpg"); Bitmap bitmap = (Bitmap)Image.FromFile("image.jpg"); bitmap.Save("image.bmp"); } public static void SetWallpaper() { SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "image.bmp", SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); } } } 

However, I observe a problem with a call of the given function. The program works without errors, however, instead of the expected result of changing the wallpaper, I get black wallpaper.

Unfortunately I do not know which way to dig.
I ask for help

  • Problems with SystemParametersInfo? A bunch of unnecessary code can be removed and a minimal reproducible example can be made. In the process I recommend adding verification of the value returned from the API and getting the error code. An error code would greatly help you understand what your problem is there. - Vladimir Martyanov
  • The environment does not issue an error code, the program does not raise exceptions. Just instead of the current desktop image becomes a black background. Is there a possibility that something was changed in the API in Windows 10? - crystal
  • MSDN here, for example, says: "If the function fails, the return value is zero. To get the extended error information, call GetLastError." - Vladimir Martyanov
  • I admit, I work with WinAPI for the first time and do not know how to do what you ask. Can you help with this? - crystal
  • 2
    I am almost 100% sure that SystemParametersInfo is trying to upload a picture not from where you saved it, and therefore just shows a black screen. Try to write the full path to the picture - Grundy

1 answer 1

In this case, the path is passed relative to the folder in which the application is running.

But, since it is not known as to what the path is taken in the SystemParametersInfo function itself, nothing is found along the specified path, and a black background color is set instead of wallpaper.

To solve the problem you need to pass the full path to the picture.