Introduction
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 your library to change it manually.
There are a few applications like the Windows 8 Bing app, that provide 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.
private async void ChangeLockScreen()
{
// Open the file open picker to get a 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 the image to lock screen.
await LockScreen.SetImageFileAsync(imageFile);
}
}
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.
Summary
In this article, we learned about How to Change the Windows 8 Lock Screen Programmatically.