How to Change the Windows 8 Lock Screen Programmatically


Are you a Windows 8 or Windows RT user? Are you looking for a code to change your Windows 8 Lock Screen programmatically? Then this article will be definitely helpful for you. Just follow the simple procedure to write an application by which you can change it easily.
Windows 8 already exposes a settings panel to change the Windows Lock Screen, but this article will help you to programmatically access that using the exposed APIs.
If you are a Windows 8 user then you are already familiar with the Windows 8 Lock Screen. If you want to change the Lock Screen then Microsoft exposes a settings panel directly from the Charms Bar under the "PC Settings" screen. There you can choose the predefined images or you can browse to your library to change it manually.
There are a few applications like the Windows 8 Bing app, that provides a button to set the Bing image as the Lock Screen background. But what about accessing that programmatically?
A Windows Store application developer might think about an application to build an app that will allow the user to set the Lock Screen background from their Facebook or SkyDrive image manually or automatically after a period of time. How to do that? In the next few minutes, we will discuss the APIs and how to do that.

Playing with the Code

The static class "LockScreen" present under the "Windows.System.UserProfile" namespace provides us APIs to play with the Windows 8 Lock Screen. It exposes an asynchronous method named SetImageFileAsync() that takes a Storage File as input. Calling this method with an appropriate parameter sets the past image as the background image of your Windows 8 Lock Screen.
To do this, first, you need to get the handle of the image as a storage file. You can use a FileOpenPicker to open up a dialog to choose the image. Once the user chooses the file, you will get the handle of the selected image as a StorageFile. Just pass it to the async method that will do the rest for you.
Here is the code snippet for your reference.
  1. private async void ChangeLockScreen()  
  2. {     // Open the file open picker to get a handle of the image  
  3.     var imagePicker =new FileOpenPicker  
  4.                             {                                  
  5.                                  ViewMode = PickerViewMode.Thumbnail,  
  6.                                 SuggestedStartLocation = PickerLocationId.PicturesLibrary,  
  7.                                 FileTypeFilter = {".jpg",".jpeg", ".png", ".bmp"},  
  8.                             };  
  9.     var imageFile = await imagePicker.PickSingleFileAsync();  
  10.     if (imageFile !=null)  
  11.     {   
  12.         // Application now has access to the picked file, setting the image to lock screen.   
  13.         await LockScreen.SetImageFileAsync(imageFile);  
  14.     }  
  15. }   
Make sure to handle the proper exceptions there since there could be possible exceptions here, like an invalid file selected by the user that was not handled. In such a case, you must provide a proper message to the end-user.
Isn't it cool? Here I just shared how to programmatically access the Windows 8 or Windows RT Lock Screen image. Time for you to integrate it by writing some additional code to connect it with Facebook or any other image sharing service. Subscribe to my blog for various articles on Windows 8 Store (WinRT), Windows Phone and other XAML/HTML5 related stuff. I am available on Twitter, Facebook, and Google+. If you are there, connect with me and share the technical stuff there.


In this article, we learned about How to Change the Windows 8 Lock Screen Programmatically.