.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Set Desktop WallPaper Using C#

Posted By:Ravi Ranjan Kumar       Posted Date: June 29, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This article enable you to learn how to set desktop wall paper using C#

.Net framework doesn't provide any class to set Desktop wallpaper by itself but is it very possible to set image using C# with the help of Platform Invocation Services  by using SystemParametersInfo function.

Take a Look how we will accomplish this task

We are going to use Platform Invocation Services , so we need to access System.Runtime.InteropServices namespace without using it we can accomplish this task

So we need to add this out program as

using System.Runtime.InteropServices;

after that

The SystemParametersInfo is found in the user32 API. To provide access to it, create a class named  "SetDWallpaper " and add the following declaration to the class: having Syntax like

public void SetDWallpaper(string path)
            // Calling SystemParametersInfo function here 
SystemParametersInfo(uint uiAction, uint uiParam, string pvParam, uint fWinIni); taking four parameter and each parameter having its appropriate task take a look below for parameter task 

a) uiAction is used To determine which item is being configured, as To change the wallpaper, the value is defined in a constant named, "SPI_SETDESKTOPWALLPAPER".

b) The The uiParam parameter is used to determine when changing the wallpaper and so should be set to zero

c) The pvParam parameter is used to determine We will pass the file path and name into it.

d) And fWinIni, determines how the change is written to user profile and whether a message should be sent to other windows to notify them of the update

As given below 


Overall program will be as given below 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ChangeDWall
    public partial class Form1 : Form
        public Form1()
        private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, string pvParam, uint fWinIni);
        const uint SPI_SETDESKWALLPAPER = 0x14;
        const uint SPIF_UPDATEINIFILE = 0x01;

        private void Form1_Load(object sender, EventArgs e)

        public void SetDWallpaper(string path)
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE);
        private void btnset_Click(object sender, EventArgs e)
        string fname = "";
        private void btnbrowse_Click(object sender, EventArgs e)
            OpenFileDialog obj = new OpenFileDialog();
            if (obj.ShowDialog() == DialogResult.OK)
                pictureBox1.Image = Image.FromFile(obj.FileName);
                fname = obj.FileName;
after accomplishment of all task our program will look like as 

Browse button is used to browse Image file from your system directory and Set Wallpaper button is used to set current image as System Desktop wallpaper.

you can download code and have a enjoy with changing wallpaper of desktop.

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend