6 Replies - 21934 Views - Last Post: 12 September 2009 - 09:36 PM Rate Topic: -----

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Get current desktop wallpaper {RESOLVED}

Posted 09 September 2009 - 02:03 PM

Ok I have the following code for retrieving the users current wallpaper

public string GetCurrentDesktopWallpaper()  
 {  
	 string currentWallpaper = new string('\0', MAX_PATH);  
	 Win32.SystemParametersInfo(SPI_GETDESKWALLPAPER, currentWallpaper.Length, currentWallpaper, 0);  
	 return currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
 }



The 2 constants are defined here

private const UInt32 SPI_GETDESKWALLPAPER = 0x73;
private const int MAX_PATH = 260;



And SystemParametersInfo Win32 API looks like this (In the class Win32 of my project)

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(UInt32 uAction, int uParam, string lpvParam, int fuWinIni);



Then in the UserControl I'm loading it like this

private DesktopManagement mgnt = new DesktopManagement();

private void WallpaperPanel_Load(object sender, EventArgs e)
{
	pbCurrentWallpaper.Image = Image.FromFile(mgnt.GetCurrentDesktopWallpaper());
}



On Windows 7 this works like a charm, but when I had someone test it on Windows XP they got

Quote

System.ArgumentException: The path is not of a legal form.


Now one would assume that if something worked on Windows 7 then it would also work on Windows XP. Anyone got any ideas why this is erroring on XP but working fine on 7?

Is This A Good Question/Topic? 0
  • +

Replies To: Get current desktop wallpaper {RESOLVED}

#2 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: Get current desktop wallpaper {RESOLVED}

Posted 09 September 2009 - 02:12 PM

Did you try getting the wallpaper from the registry?

RegistryKey UserWallpaper = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);


It seems to be an easier way for me.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Get current desktop wallpaper {RESOLVED}

Posted 09 September 2009 - 08:49 PM

Thanks for the idea Core, but the client is still getting the same error when that code runs. I think I'm going to put a MessageBox there so he can tell me then path that is being generated by the code.
Was This Post Helpful? 0
  • +
  • -

#4 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Get current desktop wallpaper {RESOLVED}

Posted 09 September 2009 - 10:55 PM

are you sure??? is it not working in windows XP????

but it works fine for me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace Wallpaper
{
    class DesktopManagement
    {
        private const UInt32 SPI_GETDESKWALLPAPER = 0x73;
        private const int MAX_PATH = 260;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(UInt32 uAction, int uParam, string lpvParam, int fuWinIni);


        public string GetCurrentDesktopWallpaper()
        {
            string currentWallpaper = new string('\0', MAX_PATH);
            SystemParametersInfo(SPI_GETDESKWALLPAPER, currentWallpaper.Length, currentWallpaper, 0);
            return currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
        }
    }
}



loading
private DesktopManagement mgnt = new DesktopManagement();

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(mgnt.GetCurrentDesktopWallpaper());
        }     

:)

---------------------------------------------------------------------------------------------------------------
i am reading wallpaper path from registry some thing like this
string wallpaper = Registry.GetValue(@"HKEY_CURRENT_USER\Control Panel\Desktop", "OriginalWallpaper",0).ToString();
pictureBox1.Image = Image.FromFile(wallpaper);

This post has been edited by noorahmad: 09 September 2009 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 Core   User is offline

  • using System.Linq;
  • member icon

Reputation: 784
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: Get current desktop wallpaper {RESOLVED}

Posted 11 September 2009 - 04:37 AM

@PsychoCoder Yeah, I would definitely implement an exception handling mechanism that would return the full error, including the current value of different variables. The code above seems to be working correctly both ways and it shouldn't cause problems in Windows XP.
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Get current desktop wallpaper {RESOLVED}

Posted 12 September 2009 - 10:43 AM

Come to find out the computer he was testing it on didn't have a wallpaper (just black screen) and I failed to implement any error checking. Got it fixed now (I have a default image in resources to account for no current wallpaper).

Thanks Core & noorahmad for your time :)
Was This Post Helpful? 0
  • +
  • -

#7 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Get current desktop wallpaper {RESOLVED}

Posted 12 September 2009 - 09:36 PM

You are welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1