There is example code available at the
MSDN page[
^]. Just download the C# source and give it a go.
In order to keep the stuff availabe when the Link should fail I wrote you a quick example up - Adjust it for yourself as ever needed.
using SDKTemplate;
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System.UserProfile;
private async void SetLockScreen()
{
rootPage.NotifyUser("", NotifyType.StatusMessage);
FileOpenPicker imagePicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" }
};
StorageFile imageFile = await imagePicker.PickSingleFileAsync();
if (imageFile != null)
{
try
{
await LockScreen.SetImageFileAsync(imageFile);
IRandomAccessStream imageStream = LockScreen.GetImageStream();
if (imageStream != null)
{
BitmapImage lockScreen = new BitmapImage();
lockScreen.SetSource(imageStream);
LockScreenImage.Source = lockScreen;
LockScreenImage.Visibility = Visibility.Visible;
}
else
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("Setting the lock screen image failed. Make sure your copy of Windows is activated.", NotifyType.StatusMessage);
}
}
catch (Exception)
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("Invalid image selected or error opening stream.", NotifyType.ErrorMessage);
}
}
else
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("No file was selected using the picker.", NotifyType.StatusMessage);
}
}
[Edit - Marco Bertschi: Added clarification and a solution to avoid the use of a FilePicker]
Please not that Windows 8 Apps which are using the new Win 8 API are only capable of accesing a previously defined list of locations on your computer. The list of currently accesible directories can be found at the
MSDN[
^]. Whatsoever, if you now would like to open a file located in your applications base directory, you would need to replace the folder picker with the following lines:
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile file = await storageFolder.GetFileAsync("yourdesiredimage.jpg");
this would lead to let your code look somewhat like this:
using SDKTemplate;
using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.System.UserProfile;
private async void SetLockScreen()
{
rootPage.NotifyUser("", NotifyType.StatusMessage);
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile imageFile= await storageFolder.GetFileAsync("yourdesiredimage.jpg");
if (imageFile != null)
{
try
{
await LockScreen.SetImageFileAsync(imageFile);
IRandomAccessStream imageStream = LockScreen.GetImageStream();
if (imageStream != null)
{
BitmapImage lockScreen = new BitmapImage();
lockScreen.SetSource(imageStream);
LockScreenImage.Source = lockScreen;
LockScreenImage.Visibility = Visibility.Visible;
}
else
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("Setting the lock screen image failed. Make sure your copy of Windows is activated.", NotifyType.StatusMessage);
}
}
catch (Exception)
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("Invalid image selected or error opening stream.", NotifyType.ErrorMessage);
}
}
else
{
LockScreenImage.Visibility = Visibility.Collapsed;
rootPage.NotifyUser("No file was selected using the picker.", NotifyType.StatusMessage);
}
}
In reply to a comment on this solution, using hard-coded full paths as "C:\Temp\Img.jpeg" is considered bad coding practice and should be avoided whenever possible. Especially Windows 8, but also Windows 7 and Vista have a way to directly access a folder by either using a framework enumeration or accessing local computer variables (such as %APPDATA%) directly to get the path of a folder.
[Edit]