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 post will be definitely helpful for you. Just follow the simple steps to write an application by which you can change it easily.


Windows 8 already exposes settings panel to change the lock screen of the Windows, but this post will help you to programmatically access that using the exposed APIs.


If you are a Windows 8 user, you already know about Windows 8 Lock Screen. If you want to change the lock screen, Microsoft exposes a settings panel directly from the Charm bar under “PC Settings” screen. There you can choose the predefined images or you can browse to your library to change it manually.


There are few applications like Windows 8 Bing app, which provides you button to set Bing image as the lock screen background. But what about accessing that programmatically?


A Windows 8 application developer might think about an application to build an app which will allow user to set the lock screen background from their Facebook or SkyDrive image manually or automatically after a period of interval. How to do that? Next few minutes we will discuss about the APIs and the way to achieve that.


Playing with the Code

The static class “LockScreen” present under “Windows.System.UserProfile” namespace provides us APIs to play with the Windows 8 Lock Screen. It exposes a asynchronous method named SetImageFileAsync() which takes Storage File as input. Calling this method with appropriate parameter sets the passed 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 storage file. You can use a FileOpenPicker to open up a dialog to choose the image. Once the user choses the file, you will get the handle of the selected image as StorageFile. Just pass it to the async method which will do the rest for you.


Here is the code snippet for your reference:


private async void ChangeLockScreen()
    // Open the file open picker to get handle of the image
    var imagePicker = new FileOpenPicker
                                ViewMode = PickerViewMode.Thumbnail,
                                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                                FileTypeFilter = {".jpg", ".jpeg", ".png", ".bmp"},
    var imageFile = await imagePicker.PickSingleFileAsync();
    if (imageFile != null)
        // Application now has access to the picked file, setting image to lockscreen.
        await LockScreen.SetImageFileAsync(imageFile);


Make sure to handle the proper exceptions there as there could be possible exceptions here like an invalid file selected by user which was not handled. In such case, you have to provide proper message to the end user.


Isn’t it cool? Here I just share you the way to programmatically access the Windows 8 or Windows RT Lock Screen image. Time for you to integrate it by writing some additional codes 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 stuffs. I am available on Twitter, Facebook and Google+. If you are there, connect with me and share the technical stuffs there.