using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace XFEToolBox.QuickTools;
public sealed class SelectionOverlay : Window
{
private readonly ScreenCapture capture; private readonly Canvas canvas = new(); private readonly Rectangle selection = new() { Stroke = Brushes.MediumPurple, StrokeThickness = 2, Fill = new SolidColorBrush(Color.FromArgb(25, 140, 120, 255)) };
private readonly TextBlock hint = new() { Foreground = Brushes.White, Background = new SolidColorBrush(Color.FromArgb(220, 20, 20, 35)), Padding = new Thickness(10), FontSize = 15, IsHitTestVisible = false };
private Point? origin; private readonly bool color;
public BitmapSource? SelectedImage { get; private set; }
public Int32Rect? SelectedPixels { get; private set; }
public string PickedColor { get; private set; } = "";
public SelectionOverlay(ScreenCapture capture, bool color = false)
{
this.capture = capture; this.color = color; WindowStyle = WindowStyle.None; ResizeMode = ResizeMode.NoResize; Topmost = true; ShowInTaskbar = false; Cursor = Cursors.Cross; Background = Brushes.Black;
var root = new Grid(); root.Children.Add(new Image { Source = capture.Image, Stretch = Stretch.Fill }); root.Children.Add(canvas); canvas.Background = Brushes.Transparent; canvas.Children.Add(selection); canvas.Children.Add(hint); Content = root;
hint.Text = color ? "点击取色 · Esc 取消" : "拖动框选 · 显示物理像素尺寸 · Esc 取消"; Canvas.SetLeft(hint, 20); Canvas.SetTop(hint, 20);
SourceInitialized += (_, _) => SetWindowPos(new WindowInteropHelper(this).Handle, new nint(-1), capture.Left, capture.Top, capture.Image.PixelWidth, capture.Image.PixelHeight, 0x40);
PreviewKeyDown += (_, e) => { if (e.Key == Key.Escape) { DialogResult = false; e.Handled = true; } };
MouseLeftButtonDown += (_, e) => { origin = e.GetPosition(canvas); CaptureMouse(); };
MouseMove += (_, e) =>
{
Point p = e.GetPosition(canvas); Rect rect = origin is Point start ? new Rect(start, p) : new Rect(p, new Size(1, 1)); var pixels = ScreenTools.PixelBounds(rect, canvas.RenderSize, capture.Image);
if (origin.HasValue) { Canvas.SetLeft(selection, rect.X); Canvas.SetTop(selection, rect.Y); selection.Width = rect.Width; selection.Height = rect.Height; }
hint.Text = $"{pixels.Width} × {pixels.Height} px {ScreenTools.ColorAt(capture.Image, pixels.X, pixels.Y)} Esc 取消";
Canvas.SetLeft(hint, Math.Clamp(p.X + 18, 0, Math.Max(0, ActualWidth - 600))); Canvas.SetTop(hint, Math.Clamp(p.Y + 18, 0, Math.Max(0, ActualHeight - 50)));
};
MouseLeftButtonUp += (_, e) =>
{
if (origin is not Point start) return; ReleaseMouseCapture(); Point end = e.GetPosition(canvas);
SelectedPixels = ScreenTools.PixelBounds(color ? new Rect(end, new Size(1, 1)) : new Rect(start, end), canvas.RenderSize, capture.Image);
PickedColor = ScreenTools.ColorAt(capture.Image, SelectedPixels.Value.X, SelectedPixels.Value.Y); SelectedImage = ScreenTools.Crop(capture.Image, SelectedPixels.Value); DialogResult = true;
};
}
[DllImport("user32.dll")] private static extern bool SetWindowPos(nint window, nint after, int x, int y, int width, int height, uint flags);
}
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace XFEToolBox.QuickTools;
public sealed class SelectionOverlay : Window
{
private readonly ScreenCapture capture; private readonly Canvas canvas = new(); private readonly Rectangle selection = new() { Stroke = Brushes.MediumPurple, StrokeThickness = 2, Fill = new SolidColorBrush(Color.FromArgb(25, 140, 120, 255)) };
private readonly TextBlock hint = new() { Foreground = Brushes.White, Background = new SolidColorBrush(Color.FromArgb(220, 20, 20, 35)), Padding = new Thickness(10), FontSize = 15, IsHitTestVisible = false };
private Point? origin; private readonly bool color;
public BitmapSource? SelectedImage { get; private set; }
public Int32Rect? SelectedPixels { get; private set; }
public string PickedColor { get; private set; } = "";
public SelectionOverlay(ScreenCapture capture, bool color = false)
{
this.capture = capture; this.color = color; WindowStyle = WindowStyle.None; ResizeMode = ResizeMode.NoResize; Topmost = true; ShowInTaskbar = false; Cursor = Cursors.Cross; Background = Brushes.Black;
var root = new Grid(); root.Children.Add(new Image { Source = capture.Image, Stretch = Stretch.Fill }); root.Children.Add(canvas); canvas.Background = Brushes.Transparent; canvas.Children.Add(selection); canvas.Children.Add(hint); Content = root;
hint.Text = color ? "点击取色 · Esc 取消" : "拖动框选 · 显示物理像素尺寸 · Esc 取消"; Canvas.SetLeft(hint, 20); Canvas.SetTop(hint, 20);
SourceInitialized += (_, _) => SetWindowPos(new WindowInteropHelper(this).Handle, new nint(-1), capture.Left, capture.Top, capture.Image.PixelWidth, capture.Image.PixelHeight, 0x40);
PreviewKeyDown += (_, e) => { if (e.Key == Key.Escape) { DialogResult = false; e.Handled = true; } };
MouseLeftButtonDown += (_, e) => { origin = e.GetPosition(canvas); CaptureMouse(); };
MouseMove += (_, e) =>
{
Point p = e.GetPosition(canvas); Rect rect = origin is Point start ? new Rect(start, p) : new Rect(p, new Size(1, 1)); var pixels = ScreenTools.PixelBounds(rect, canvas.RenderSize, capture.Image);
if (origin.HasValue) { Canvas.SetLeft(selection, rect.X); Canvas.SetTop(selection, rect.Y); selection.Width = rect.Width; selection.Height = rect.Height; }
hint.Text = $"{pixels.Width} × {pixels.Height} px {ScreenTools.ColorAt(capture.Image, pixels.X, pixels.Y)} Esc 取消";
Canvas.SetLeft(hint, Math.Clamp(p.X + 18, 0, Math.Max(0, ActualWidth - 600))); Canvas.SetTop(hint, Math.Clamp(p.Y + 18, 0, Math.Max(0, ActualHeight - 50)));
};
MouseLeftButtonUp += (_, e) =>
{
if (origin is not Point start) return; ReleaseMouseCapture(); Point end = e.GetPosition(canvas);
SelectedPixels = ScreenTools.PixelBounds(color ? new Rect(end, new Size(1, 1)) : new Rect(start, end), canvas.RenderSize, capture.Image);
PickedColor = ScreenTools.ColorAt(capture.Image, SelectedPixels.Value.X, SelectedPixels.Value.Y); SelectedImage = ScreenTools.Crop(capture.Image, SelectedPixels.Value); DialogResult = true;
};
}
[DllImport("user32.dll")] private static extern bool SetWindowPos(nint window, nint after, int x, int y, int width, int height, uint flags);
}