Change windows lock screen image - Microsoft.Windows.SDK.Contracts

Saud Khan 26 Reputation points
2022-01-24T13:11:24.073+00:00

I'm trying to change lock screen images in the windows form application by using Microsoft.Windows.SDK.Contracts. First time I have tried this code that was written for the UWP app and working fine there:

StorageFolder tempFolder = ApplicationData.Current.LocalFolder;

        if (!Directory.Exists($"{tempFolder.Path}\\screensaver"))
            tempFolder = await tempFolder.CreateFolderAsync("screensaver");
        else
            tempFolder = await tempFolder.GetFolderAsync("screensaver");

        //getting user's saved images from temp
        var userImages =await tempFolder.GetFilesAsync();

        if (userImages.Count > 0)
        {
            foreach(var photo in userImages)
            {
                IRandomAccessStream imgStream = await photo.OpenAsync(FileAccessMode.Read);
                await LockScreen.SetImageStreamAsync(imgStream);

                await Task.Delay(TimeSpan.FromSeconds(5));
            }
        }
        else
        {
            int count = 2;
            foreach (var photo in photos)
            {
                IRandomAccessStreamReference thumbnail =
                RandomAccessStreamReference.CreateFromUri(new Uri(photo));
                StorageFile imageFile = await StorageFile.CreateStreamedFileFromUriAsync($"photo{count}.png", new Uri(photo), thumbnail);
                try
                {
                    if (imageFile != null)
                    {
                        var newImgFile = await imageFile.CopyAsync(tempFolder);
                        IRandomAccessStream imgStream = await newImgFile.OpenAsync(FileAccessMode.Read);
                        await LockScreen.SetImageStreamAsync(imgStream);

                        await Task.Delay(TimeSpan.FromSeconds(5));
                    }
                }
                catch (Exception ex)
                {
                    await new MessageDialog(ex.Message).ShowAsync();
                }
                count = count + 1;
            }
        }

But when I tried this code into the windows form application I got an error on this line.

StorageFolder tempFolder = ApplicationData.Current.LocalFolder;

The process has no package identity

So I replaced that line with this one and the error has gone:

StorageFolder tempFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetTempPath());

But now the issue is that it is not changing the lock screen image although the images are saved under the temp folder and there's no exception in the code.

I also tried to create a folder under C:\Users\name\AppData\Local\Packages\build-Id\LocalState same as the UWP application does but no success.

Looking forward to an expert opinion.

Thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,802 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,360 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,972 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 80,911 Reputation points
    2022-01-24T14:55:24.323+00:00

    This simplified test works for me (Windows 10 21H1), as x64,
    with a PictureBox to display the new image =>

    At beginning :

    // Add reference to : "C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd"
    // For .AsStream()
    // Add reference to : "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll" 
    
    
    // For .AsStream()
    using System.IO;
    using System.Runtime.InteropServices.WindowsRuntime;
    

    Test code :

    string sFilename = @"E:\Tools\Graphism\Images\Paradise.jpg";
    Windows.Storage.StorageFile imageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(sFilename);           
    await Windows.System.UserProfile.LockScreen.SetImageFileAsync(imageFile);
    
    Uri sImage = Windows.System.UserProfile.LockScreen.OriginalImageFile;
    Windows.Storage.Streams.IRandomAccessStream stream = Windows.System.UserProfile.LockScreen.GetImageStream();
    using (StreamReader sr = new StreamReader(stream.AsStream()))
    {
        this.pictureBox1.Image = System.Drawing.Image.FromStream(sr.BaseStream);
    }
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    

0 additional answers

Sort by: Most helpful