Few days ago, I wrote a blog post about Windows 10 Start Screen image programmatically using C# code. Today in this blog post, we will discuss about changing Lock Screen background image of Windows 10 using C# code. Microsoft Windows SDK provides the API sets to call.

 

Let’s start learning about the API with a small piece of code. Continue reading. Code has been shared at the end of the post. If you have any comments, drop a line.

 

#UWP Tips: C# code to change #Windows 10 Lock Screen image (www.kunal-chowdhury.com)

 

Windows 10 SDK provides API class “UserProfilePersonalizationSettings” under the “Windows.System.UserProfile” namespace. This class is sealed and hence you cannot inherit it. The class provides three methods and one property to handle user profile personalization.

 

The asynchronous method “TrySetWallpaperImageAsync” provided by the API allows you to set the background image of the start screen. More about this is available in this post. The asynchronous method “TrySetLockScreenImageAsync” provided by the API allows you to set the background image of the lock screen. You have to pass the object of the image as a StorageFile and returns the success status as a boolean value.

 

private static async Task ChangeLockScreenBackground()
{
   if (UserProfilePersonalizationSettings.IsSupported())
   {
      StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(ImagePath));
      UserProfilePersonalizationSettings settings = UserProfilePersonalizationSettings.Current;
      await settings.TrySetLockScreenImageAsync(file);
   }
}

 

First you need to check whether the system supports user profile personalization. You can check it by calling the static method UserProfilePersonalizationSettings.IsSupported(), as shown in the above code snippet. Then get the StorageFile object of the image that you want to set. You can get the instance by calling StorageFile.GetFileFromApplicationUriAsync method with the URI of the image. You can also call StorageFile.GetFileFromPathAsync method to get the image file.

 

At the end call the TrySetLockScreenImageAsync method of the current UserProfilePersonalizationSettings class by passing the image file. When called, it will change the image background and return true/false based on the success.

 

Though this post was small but I hope, it will help you to understand the API, implement the code in your app and allow your app to change the background image of users lock screen. This works in both Windows 10 PC and Windows 10 Mobile.

 

 

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.