-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockScreenMenuButtonGenerator.cs
More file actions
86 lines (74 loc) · 2.74 KB
/
LockScreenMenuButtonGenerator.cs
File metadata and controls
86 lines (74 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Wbooru.UI.Controls;
using Wbooru.UI.Controls.PluginExtension;
using Wbooru.UI.Dialogs;
using Wbooru.UI.Pages;
using Wbooru.Utils;
using Windows.System.UserProfile;
namespace WbooruPlugin.WindowsStyleCustomizer
{
[Export(typeof(IExtraDetailImageMenuItem))]
public class LockScreenMenuButtonGenerator : IExtraDetailImageMenuItem
{
public UIElement Create()
{
var button = new MenuItem()
{
Header = "设置为锁屏壁纸"
};
button.Click += SetLockScreenBackground;
return button;
}
private async void SetLockScreenBackground(object sender, RoutedEventArgs _)
{
var picture_page = (sender as FrameworkElement)?.DataContext as FrameworkElement;
if (!(VisualTreeHelperEx.Find(a => a is ImageViewer, picture_page) is ImageViewer viewer))
{
Toast.ShowMessage($"找不到依赖的ImageViewer控件");
return;
}
if (!(viewer.ImageSource is BitmapSource source))
{
Toast.ShowMessage($"无法从ImageViewer控件提取图片");
return;
}
if (!await Dialog.ShowComfirmDialog("是否将此图片设置成锁屏壁纸?"))
return;
try
{
using (MemoryStream ms = new MemoryStream())
{
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(ms);
/*
using (var bitmap = new Bitmap(System.Drawing.Image.FromStream(ms)))
{
var path = Path.GetTempFileName();
bitmap.Save(path);
var file = await StorageFile.GetFileFromPathAsync(path);
await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(file);
}
*/
await LockScreen.SetImageStreamAsync(ms.AsRandomAccessStream());
Toast.ShowMessage("设置成功");
}
}
catch (Exception e)
{
Toast.ShowMessage("设置失败:" + e.Message);
}
}
}
}