0

I started a windows form application which imports images from files and store them and specify a radio button to each one.

it have a button with the name 'Lock' so if user select one radio button and then press the button the app should change the lock screen image and then Lock the screen.

But I don't know how to set the image and lock the screen.

I googled later and the answer about the LockScreen.blabla didn't work for me because I can't do using windows.system.userprofile;

If some one get me the assembly i will do the thing.

there is the events:

private void rB_CheckedChanged(object sender, EventArgs e)
        {
            MyRadioButton radioButton = sender as MyRadioButton;
            pictureBox1.Image = radioButton.Image;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                foreach (Control control in FindForm().Controls)
                {
                    if (control.GetType() == typeof(MyRadioButton))
                    {
                        MyRadioButton mrb = control as MyRadioButton;
                        if (mrb.Checked == true)
                        {
                            mrb.Image = pictureBox1.Image;
                        }
                    }
                }
            }
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            //should set the lock screen background

            LockScreen.LockScreen.SetImageFileAsync(imagefile);//shows error 'the name lock screen does not exist
        }
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Well, you cannot reference `Windows.System.Userprofile` in a WinForms application in Windows 8.1. You'ld need Windows 10 SDK and Windows 10 1903. You can look at the Registry, though. Locking the screen itself can be done, instead: [LockWorkStation](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-lockworkstation). – Jimi Aug 28 '19 at 18:33
  • So in what kind of app I can import that? Maybe windows store – Difender Amini Aug 29 '19 at 07:42
  • That would be a Universal Windows 8 app (Windows Store or Windows RT Shared). See [Build a Universal Windows 8 app](https://learn.microsoft.com/en-us/previous-versions/windows/apps/br211384(v=win.10)). What API are supported in Windows 8.1 is a little sketchy. You'll have to find out (I, personaly, completely abandoned this technology a while ago). – Jimi Aug 29 '19 at 11:04
  • Thanks @Jimi I will answer my question when I changed the lockscreen background via an app. – Difender Amini Aug 29 '19 at 19:10
  • Hey guys I made a new app using UWP But there is another problem about locking the screen. as ` @Jimi ` said I tried LockWorkStation But in c# the solution [here](https://stackoverflow.com/questions/1263047/lock-windows-workstation-programmatically-in-c-sharp) But this method keeps throwing 1008 win32 error about a token (I don't know) can you help me in this or I should make a new thread for that. – Difender Amini Aug 30 '19 at 22:27

1 Answers1

0

I have made a UWP application to use the LockScreen class provided by windows.userProfile.dll And the event's changed to:


        private Dictionary<string, string> Images = new Dictionary<string, string>();
        private async void btnLock_Click(object sender, RoutedEventArgs e)
        {
            string path;
            if (Images.TryGetValue(selectedRadioButton.Name, out path))
            {
                StorageFile file = await StorageFile.GetFileFromPathAsync(path);
                await LockScreen.SetImageFileAsync(file);
                try
                {
                    HttpClient httpClient = new HttpClient();
                    Uri uri = new Uri("http://localhost:8080/lock/");

                    HttpStringContent content = new HttpStringContent(
                        "{ \"pass\": \"theMorteza@1378App\" }",
                        UnicodeEncoding.Utf8,
                        "application/json");

                    HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
                        uri,
                        content);

                    httpResponseMessage.EnsureSuccessStatusCode();
                    var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

private async void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");

            StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                using (var imageStream = await file.OpenReadAsync())
                {
                    var bitmapImage = new BitmapImage();
                    await bitmapImage.SetSourceAsync(imageStream);
                    image.Source = bitmapImage;
                }
                StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
                await file.CopyAsync(storageFolder, selectedRadioButton.Name+file.FileType,NameCollisionOption.ReplaceExisting);
                addOrUpdate(selectedRadioButton.Name, Path.Combine(storageFolder.Path, selectedRadioButton.Name + file.FileType));
                save();
            }
        }
        private async void rb_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            selectedRadioButton = rb;
            btnBrowse.IsEnabled = true;
            string path;
            if (Images.TryGetValue(rb.Name, out path))
            {
                StorageFile file = await StorageFile.GetFileFromPathAsync(path);
                using (var imageStream = await file.OpenReadAsync())
                {
                    var bitmapImage = new BitmapImage();
                    await bitmapImage.SetSourceAsync(imageStream);
                    image.Source = bitmapImage;
                }
            }
        }
        private void addOrUpdate(string key, string image)
        {
            if (Images.Keys.Contains(key))
            {
                Images.Remove(key);
                Images.Add(key, image);
            }
            else
            {
                Images.Add(key, image);
            }
        }

        private async void save()
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile file = await storageFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(Images));
        }

        private async void load()
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile sampleFile = await storageFolder.GetFileAsync("sample.txt");
            string fileText = await FileIO.ReadTextAsync(sampleFile);
            try
            {
                Images = JsonConvert.DeserializeObject<Dictionary<string, string>>(fileText);
            }
            catch (Exception)
            {
            }
        }

and you can see in the lock button click event there is a request to a web server cause you cannot directly lock the screen in a UWP app and you can follow the rest of your question in my next answer to the question "why I can't directly lock screen in UWP app?and how to do so?".