#UWP Tips: C# code to change #Windows 10 Lock Screen image


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? Drop a line below and I will try to answer you as soon as possible. Don’t forget to subscribe to my blog’s RSS feed and email newsletter. If you are on Twitter, Facebook or Google+, do connect with me. Also, I have a plenty of article on Windows 10 Tips and Tricks which you can read here: http://win10tips.kunal-chowdhury.com

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

3 comments

  1. hey... whats imagepath? can u share that code plz?

    ReplyDelete
  2. how do you handle different resolutions? and scale factors?

    ReplyDelete

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top