65.9K
CodeProject is changing. Read more.
Home

Desktop Decorator : Changing your wallpaper the easy way in .NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.14/5 (15 votes)

Apr 19, 2004

1 min read

viewsIcon

112756

downloadIcon

3602

An article describing how to change the wallpaper using .NET and C#

Introduction

This small article describes how to set the desktop wallpaper. There's really not much to it. The sample application is a Windows Forms application and uses the "Wallpaper" class to set the wallpaper. The class can set wallpaper from most image sources, including images stored locally or on the network/internet. To use the sample application, run it, select an image, and click the Apply button. To test the URL loading, try selecting an image from a web-page, for example, http://hp.msn.com/global/c/lg/msft_118x35.gif

Using the code

The class for setting the desktop wallpaper is named, not surprisingly, "Wallpaper". It contains one public method name 'Set'. This takes two parameters, a URI that points to the image you want to use, and a "Style" enumeration that specifies how you want the image displayed (tiled, centered, or stretched).

public sealed class Wallpaper 
{
 ...
  public enum Style : int 
  { 
   Tiled, 
   Centered, 
   Stretched 
  } 

  public static void Set ( Uri uri, Style style ) 
  {
    ...
  } 
}

Points of Interest

The class first saves your image to a temporary file using a Windows Bitmap format. This allows you to pass other image formats, such as a gif and jpeg files and have them automatically converted and used as wallpaper images. The class also allows source images from almost any location. A URI is passed which describes where the image is located. Feel free to use this code however you please. The usual stuff: if it buggers up your machine/life etc. then it's not my fault!