Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to change Windows 8 lockscreen from wpf application.

I figured out way to use lockscreen class from .netcore but those methods take IStorage as arguments.
I am new to c# and I dont know how to assign files to Istorage.

How can I load file in Istorage variable???

Thank You
Posted

1 solution

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.

C#
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 
         { 
            // Application now has access to the picked file, setting image to lockscreen.  This will fail if the file is an invalid format. 
            await LockScreen.SetImageFileAsync(imageFile); 
 
            // Retrieve the lock screen image that was set 
            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:
C#
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 
         { 
            // Application now has access to the picked file, setting image to lockscreen.  This will fail if the file is an invalid format. 
            await LockScreen.SetImageFileAsync(imageFile); 
 
            // Retrieve the lock screen image that was set 
            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]
 
Share this answer
 
v2
Comments
Member 10518749 15-Jan-14 6:44am    
I dont want that image picker.

I was trying to assign file directly to "StorageFile imageFile" present in one of the folder like c:\image.jpg.

is there any way I can do it.
Marco Bertschi 15-Jan-14 14:13pm    
I updated the solution above with a second code sample. It doesn't seem to be possible to access a path directly via "C:\image.jpeg", but you can deploy the image with your application and then look in the "Windows.ApplicationModel.Package.Current.InstalledLocation" StorageFolder, or the Documents folder, or the downloads folder, or... - You get my point. I have also added a list / explanation of accesible folders with the Win 8 API (http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx). Now it is on you again, get into it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900