Change Windows 8 Lock Screen Programmatically in Windows Store Apps

Introduction

Today we explain how to change the Windows 8 Lock screen programmability in Windows Store apps. If you are a Windows 8 user then you already know about the Windows 8 Lock Screen. If you want to change the lock screen manually then go to "Charms Bar" -> "Settings" -> "Change PC Settings" -> "Personalize" -> "Select image for Lock Screen". There are a few applications, like the Windows 8 Bing app, that provides you a button to set the Bing image as the lock screen background.

But if you want to do it using programming then we use the "LockScreen" class available in the "Windows.System.UserProfile" namespace that provides APIs to work 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 specified image as the background image of your Windows 8 lock screen. To select an image from the local drive we are using the "Windows.Storage.Pickers" namespace.

Step 1

Open Visual Studio 2012 and start a new Windows Store apps project.

Step 2

Go to Solution Explorer and double-click on "MainPage.xaml" to open it.

Solution-Explorer-Windows-Store-Apps.png

Step 3

In the "MailPage.xaml" page we will use a Button control named "ChangeLockScreen".

<Button Content="ChangeLockScreen" HorizontalAlignment="Left" x:Name="ChangeLockScreen" VerticalAlignment="Top"/>

Step 4

For the "ChangeLockScreen" button click event write the following code:

private async void ChangeLockScreen_Click(object sender, RoutedEventArgs e)

  {

    var imagePicker = new FileOpenPicker

      {

        ViewMode = PickerViewMode.Thumbnail,

        SuggestedStartLocation = PickerLocationId.PicturesLibrary,

        FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" },

      };

 

     var MyImage = await imagePicker.PickSingleFileAsync();

     if (MyImage != null)

      {

        await LockScreen.SetImageFileAsync(MyImage);

      }

   }

Step 5

Now run the program and click on the "ChangeLockScreen" button.

Result-Windows-Store-Apps.png

Step 6

The File Explorer will be opened. Select an image that you want set as the lock screen and click on "Open".

SelectFile-Windows-Store-Apps.png

Step 7

Now go to the lock screen to see the change.


Similar Articles