using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using Microsoft.Win32; namespace XFEToolBox.QuickTools; public sealed partial class QuickToolPage { private void BuildScreen() { BitmapSource? current = null; var result = Field("ScreenResult", "", true, true); result.MinHeight = 85; result.Height = 100; var image = new Image { Stretch = Stretch.None, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top }; RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor); var preview = new ScrollViewer { Content = image, HorizontalScrollBarVisibility = ScrollBarVisibility.Auto, VerticalScrollBarVisibility = ScrollBarVisibility.Auto }; var empty = Stack(new TextBlock { Text = "▣", FontSize = 50, Foreground = System.Windows.Media.Brushes.LightSlateGray, HorizontalAlignment = HorizontalAlignment.Center }, new TextBlock { Text = "截图、取色、测量,都在这张画布上", Foreground = System.Windows.Media.Brushes.White, FontSize = 18 }, new TextBlock { Text = "从上方开始框选,或拖入一张图片", Foreground = System.Windows.Media.Brushes.LightGray, Margin = new Thickness(0, 12, 0, 0), HorizontalAlignment = HorizontalAlignment.Center }); empty.HorizontalAlignment = HorizontalAlignment.Center; empty.VerticalAlignment = VerticalAlignment.Center; var canvas = new Grid { Name = "ScreenCanvas", Height = 305, Background = new SolidColorBrush(Color.FromRgb(27, 32, 47)) }; canvas.Children.Add(preview); canvas.Children.Add(empty); var dimensions = Label("尚无选区", 14); var swatch = new Border { Width = 24, Height = 24, CornerRadius = new CornerRadius(6), Background = Tint, Margin = new Thickness(8, 0, 8, 0) }; var zoom = new Slider { Minimum = 1, Maximum = 8, Value = 1, TickFrequency = 1, IsSnapToTickEnabled = true, Width = 155, Margin = new Thickness(10, 8, 10, 8), ToolTip = "像素放大 1–8 倍" }; var scale = Label("1×", 14); zoom.ValueChanged += (_, _) => { image.LayoutTransform = new ScaleTransform(zoom.Value, zoom.Value); scale.Text = zoom.Value.ToString("0") + "×"; }; void Show(BitmapSource bitmap) { current = bitmap; image.Source = bitmap; empty.Visibility = Visibility.Collapsed; zoom.Value = 1; dimensions.Text = $"{bitmap.PixelWidth} × {bitmap.PixelHeight} px"; } async Task Load(string file) { Show(await Task.Run(() => QrService.Load(file), cancellation.Token)); result.Clear(); status.Text = "已加载图片,可进行 OCR、像素放大或贴图。"; } async Task Capture(bool color, bool ocr) { var owner = Window.GetWindow(this)!; owner.Hide(); try { await Task.Delay(180, cancellation.Token); var shot = ScreenTools.CaptureDesktop(); var overlay = new SelectionOverlay(shot, color); if (overlay.ShowDialog() != true) { status.Text = "已取消框选。"; return; } if (overlay.SelectedImage is not { } bitmap) return; Show(bitmap); result.Text = dimensions.Text + "\n" + overlay.PickedColor; if (overlay.PickedColor.Length >= 7) swatch.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(overlay.PickedColor[..7])); if (ocr) result.Text = await ScreenTools.Recognize(bitmap, token: cancellation.Token); status.Text = color ? "已取色,颜色值显示在结果区。" : "已捕获选区,可保存、识别或贴在屏幕上。"; } finally { if (!cancellation.IsCancellationRequested) { owner.Show(); owner.Activate(); } } } body.Children.Add(Actions(Primary("框选截图 / 测量", () => Capture(false, false)), ActionButton("框选 OCR", () => Capture(false, true)), ActionButton("屏幕取色", () => Capture(true, false)), ActionButton("加载图片", async () => { var file = ChooseFile("图片|*.png;*.jpg;*.jpeg;*.bmp|所有文件|*.*"); if (file is not null) await Load(file); }))); DropFiles(canvas, paths => { var file = paths.FirstOrDefault(File.Exists); if (file is not null) _ = Run(() => Load(file)); }); body.Children.Add(canvas); body.Children.Add(Actions(dimensions, swatch, Hint("像素放大"), zoom, scale)); body.Children.Add(Actions(ActionButton("复制图片", () => { if (current is null) throw new InvalidOperationException("没有选区图片。"); Clipboard.SetImage(current); status.Text = "已复制图片。"; }), ActionButton("保存 PNG", () => { if (current is null) throw new InvalidOperationException("没有图片。"); var dialog = new SaveFileDialog { Filter = "PNG 图片|*.png", FileName = "截图.png", OverwritePrompt = false }; if (dialog.ShowDialog(Window.GetWindow(this)) == true) { QrService.Save(current, dialog.FileName); status.Text = "图片已保存。"; } }), ActionButton("贴在屏幕上", () => { if (current is null) throw new InvalidOperationException("没有可贴出的图片。"); var pinned = new Window { Title = "XFE 贴图 · 拖动移动,Esc 关闭", Topmost = true, Width = Math.Clamp(current.PixelWidth + 20, 160, 900), Height = Math.Clamp(current.PixelHeight + 50, 120, 700), Content = new Image { Source = current, Stretch = Stretch.Uniform }, Background = System.Windows.Media.Brushes.Black, WindowStartupLocation = WindowStartupLocation.CenterScreen }; pinned.MouseLeftButtonDown += (_, e) => { if (e.ButtonState == MouseButtonState.Pressed) pinned.DragMove(); }; pinned.KeyDown += (_, e) => { if (e.Key == Key.Escape) pinned.Close(); }; pinned.Show(); status.Text = "贴图已置顶;Esc 关闭贴图。"; }))); body.Children.Add(Section("识别文字 / 颜色与尺寸", Actions(Primary("识别当前图片", async () => { if (current is null) throw new InvalidOperationException("请先框选或加载图片。"); Result(await ScreenTools.Recognize(current, token: cancellation.Token), result); }), Copy(result)), result)); status.Text = "Esc 取消框选 · Windows 本地 OCR · 不上传截图"; } }