This post is about setting Bing wallpaper as your desktop wallpaper using C# and Windows Forms. Bing.com is famous for having some nice pictures as wallpaper, updated on daily basis. The Bing Desktop application comes with same feature. Here is the code snippet, which has two parts one, download the wallpaper image from bing.com and set it as wallpaper using WIN32 APIs.
The URL for getting wallpaper is used by bing WP7 application.
In the URL, you can change the orientation to 1024×768, landscape mode in Windows Phone to get the wallpaper. And the mkt parameter specifies the region, I am keep it as en-US. The following snippet will download the wallpaper and save it in the Temp folder as wallpaper.bmp. The SetWallpaper function will set the wallpaper.bmp as desktop wallpaper using Win32 API.
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += (o, ev) =>
if (ev.Error == null)
var wallpaper = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
using (var bitmap = Bitmap.FromStream(ev.Result))
You require following WIN32 API declarations as well.
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);
In the SetWallpaper function, I am setting the wallpaper picture position as Stretched. You can change this by changing the WallpaperStyle and TileWallpaper values.
Sometimes you may need to change the startup page of Windows Phone application. By default it will be pointing to MainPage.xaml. You can modify the WMAppManifest.xml to change the default page. In the WMAppManifest.xml you will find the following.(You can get the WMAppManifest.xml by expanding the properties node in the Project Explorer window.)
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>
If you want to display an exit confirmation from your Windows Phone app, if user press the hardware back button, here is the code snippet. (It is not a recommended practice, as per Windows Phone market place certification requirements, if user pressing the back button from the application’s first page, application should exit)
protected override void OnBackKeyPress(CancelEventArgs e)
if (MessageBox.Show("Are you sure want to exit?", "Confirm",
MessageBoxButton.OKCancel) != MessageBoxResult.OK)
e.Cancel = true;
It will display a message box like this, if user press the back button.