Thanks to everyone for your help! Special thanks to Jim Mischel for pointing me in the right direction.
In WPF, there's a handy method called PointToScreen that helped me solve the issue. This method converts a point relative to a WPF element to screen coordinates, which was exactly what I needed.
using System;
using System.Drawing; // For screen capturing (Bitmap, Graphics)
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ScreenCapture
{
public partial class MainWindow : Window
{
private System.Windows.Shapes.Rectangle DragRectangle = null; // Specify WPF Rectangle
private System.Windows.Point StartPoint, LastPoint;
public MainWindow()
{
InitializeComponent();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) { this.Close(); e.Handled = true; }
}
private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
StartPoint = Mouse.GetPosition(canvas);
LastPoint = StartPoint;
// Initialize and style the rectangle
DragRectangle = new System.Windows.Shapes.Rectangle
{
Width = 1,
Height = 1,
Stroke = System.Windows.Media.Brushes.Red,
StrokeThickness = 1,
Cursor = Cursors.Cross
};
// Add the rectangle to the canvas
canvas.Children.Add(DragRectangle);
Canvas.SetLeft(DragRectangle, StartPoint.X);
Canvas.SetTop(DragRectangle, StartPoint.Y);
// Attach the mouse move and mouse up events
canvas.MouseMove += canvas_MouseMove;
canvas.MouseUp += canvas_MouseUp;
canvas.CaptureMouse();
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (DragRectangle == null) return;
// Update LastPoint to current mouse position
LastPoint = Mouse.GetPosition(canvas);
// Update rectangle dimensions and position
DragRectangle.Width = Math.Abs(LastPoint.X - StartPoint.X);
DragRectangle.Height = Math.Abs(LastPoint.Y - StartPoint.Y);
Canvas.SetLeft(DragRectangle, Math.Min(LastPoint.X, StartPoint.X));
Canvas.SetTop(DragRectangle, Math.Min(LastPoint.Y, StartPoint.Y));
}
private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
if (DragRectangle == null) return;
// Release mouse capture and remove event handlers
canvas.ReleaseMouseCapture();
//canvas.MouseMove -= canvas_MouseMove;
//canvas.MouseUp -= canvas_MouseUp;
// Capture the screen area based on the selected rectangle
CaptureScreen();
// Clean up: remove rectangle from canvas
canvas.Children.Remove(DragRectangle);
DragRectangle = null;
this.Close();
}
private void CaptureScreen()
{
// Convert StartPoint and LastPoint to screen coordinates
var screenStart = PointToScreen(StartPoint);
var screenEnd = PointToScreen(LastPoint);
// Calculate the capture area dimensions and position
int x = (int)Math.Min(screenStart.X, screenEnd.X);
int y = (int)Math.Min(screenStart.Y, screenEnd.Y);
int width = (int)Math.Abs(screenEnd.X - screenStart.X);
int height = (int)Math.Abs(screenEnd.Y - screenStart.Y);
// Ensure dimensions are valid before capture
if (width > 0 && height > 0)
{
using (Bitmap bitmap = new Bitmap(width, height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
// Capture the area of the screen based on converted coordinates
g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
}
// Save the captured image to the desktop
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//string filePath = System.IO.Path.Combine(desktopPath, $"Screenshot_{DateTime.Now:yyyyMMdd_HHmmss}.png");
string filePath = System.IO.Path.Combine(desktopPath, "Screenshot.png");
bitmap.Save(filePath);
System.Diagnostics.Process.Start(filePath);
//MessageBox.Show($"Screenshot saved to {filePath}");
}
}
}
}
}