Change desktop wallpaper using code in .NET
Here's a class yanked from an app I wrote a year or two ago:
public sealed class Wallpaper
{
Wallpaper() { }
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public static void Set(Uri uri, Style style)
{
System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
tempPath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
I haven't tested it extensively, so use at your own risk.
Change desktop background in C#
pvParam
should be a local file. It will not work for urls...
First download the image, then give its local path to SystemParametersInfo
method.
var filename = "4.jpg";
new WebClient().DownloadFile("http://www.scottgames.com/4.jpg", filename);
SystemParametersInfo(SPI_SETDESKWALLPAPER, 1, filename, SPIF_UPDATEINIFILE);
Changing the desktop wallpaper using C# currently displays tiny image
You need to use your registry key to set the style of background to "Stretched".
After this line
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
Add
key.SetValue("WallpaperStyle", "2");
key.SetValue("TileWallpaper", "0");
How do I change my Windows desktop wallpaper programmatically?
Well, this is a bit awkward, but I'll answer my own question with what I found.
I had to reuse more code from the accepted answer here.
Basically the problem in XP was that it needed to use a bmp file, so I managed to convert a project resource to a bmp file using that previous example and a little of tweaking. The Set method works perfectly this way:
public static void Set(string wpaper, Style style)
{
using(System.Drawing.Image img = System.Drawing.Image.FromFile(Path.GetFullPath(wpaper)))
{
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
tempPath,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
The important part is on the third line of this code (System.Drawing.Image.FromFile(Path.GetFullPath(wpaper));
).
Change Desktop background using VB.NET
Steps you will do.
Start visual studio 2005 and create a new window project.
Set the following properties of the form
'Text = "Set Wallpaper"
'Size = “1024,750”
Now drip a picture box control on the form and set its following properties.
'Size = “1024,725”
'Sizemode = ”centerimage”
Drop a two button controls on the form and set its following properties as below.
'First button control.
'Name = " btgetimage"
'Text = " Brows For Image"
'Second button control.
'Name = " btsetwallpaper"
'Text = " Set Wallpaper"
Now drop an openfiledialog control on the form.
Open you code window and import the following namespace.
Imports System.IO.Directory
Now declare the function and variables as below which will use win API's to set the wallpaper.
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDWININICHANGE As Integer = &H2
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
Const WallpaperFile As String = "c:\wallpaper.bmp"
Make a function as below.
Friend Sub SetWallpaper(ByVal img As Image)
Dim imageLocation As String
imageLocation = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperFile)
Try
img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp)
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
Catch Ex As Exception
MsgBox("There was an error setting the wallpaper: " & Ex.Message)
End Try
End Sub
Now in the click event of the first button write the following code to open and get the image.
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "JPG|*.jpg|Bitmap|*.bmp"
Dim dialogresult As DialogResult = OpenFileDialog1.ShowDialog
If dialogresult = Windows.Forms.DialogResult.OK Then
PictureBox1.ImageLocation = OpenFileDialog1.FileName
btsetwallpaper.Enabled = True
End If
In the click event of the second button write following code to set the wallpaper.
SetWallpaper(Me.PictureBox1.Image)
MessageBox.Show("Wallpaper has been changed", "Set Wallpaper", MessageBoxButtons.OK, MessageBoxIcon.Information)
Related Topics
How to Remove Whitespace on Merge
How to Calculate the Angle Between a Line and the Horizontal Axis
How to Populate/Instantiate a C# Array with a Single Value
Call Static Method with Reflection
Making Entity Class Closed for Changes
Multi-Threaded Splash Screen in C#
Replace First Occurrence of Pattern in a String
SQL Command Insert Is Working But the Data Not Appear in Table
How Does the Ternary Operator Work
Pass Complex Parameters to [Theory]
Is the Use of Dynamic Considered a Bad Practice
Adding Items to a List<> of Objects Results in Duplicate Objects When Using a New in a Loop