[https://www.cnblogs.com/yang-fei/p/4029782.html] 代码引用这里,感谢作者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Forms;
namespace CaptureWindow
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private double x;
private double y;
private double width;
private double height;
bool savePicture_flag = false;
private bool isMouseDown = false;
private void savePicture_Click(object sender, RoutedEventArgs e)
{
savePicture_flag = true;
}
public MainWindow()
{
InitializeComponent();
}
private void CaptureWindow_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
isMouseDown = true;
x = e.GetPosition(null).X; //返回鼠标指针相对于指定元素(在此即原点)的位置
y = e.GetPosition(null).Y;
}
private void CaptureWindow_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (isMouseDown)
{
//通过一个矩形来表示目前截图区域
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
double dx = e.GetPosition(null).X; //返回鼠指针相对于指定元素(此位置为0)的位置
double dy = e.GetPosition(null).Y;
double rectWidth = Math.Abs(dx - x); //返回双精度浮点数的绝对值
double rectHeight = Math.Abs(dy - y);
SolidColorBrush brush = new SolidColorBrush(Colors.Black); //用纯色画一个区
rect.Width = rectWidth;
rect.Height = rectHeight;
rect.Fill = brush; //填充:形状的内部被涂上
rect.Stroke = brush; //绘图边框
rect.StrokeThickness = 1;
if (dx < x) //区域选择就在这里
{
Canvas.SetLeft(rect, dx); //为给定的从属对象设置左侧附属属性的值。
Canvas.SetTop(rect, dy);
}
else
{
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
}
CaptureCanvas.Children.Clear();
CaptureCanvas.Children.Add(rect); //添加指定的元素,即在截图时出现矩形选框
if (e.LeftButton == MouseButtonState.Released) //判断鼠标左键是当前不是等于鼠标释放的状态
{
CaptureCanvas.Children.Clear();
//获取当前截图区域
width = Math.Abs(e.GetPosition(null).X - x); //确定了x的绝对差值
height = Math.Abs(e.GetPosition(null).Y - y);
if (e.GetPosition(null).X > x)
{
CaptureScreen(x, y, width, height); // 将颜色数据从屏幕转移到图形的绘图表面。CaptureScreen是Graphics的方法
}
else
{
CaptureScreen(e.GetPosition(null).X, e.GetPosition(null).Y, width, height);
}
isMouseDown = false;
x = 0.0;
y = 0.0;
this.Close();
}
}
}
private void CaptureScreen(double x, double y, double width, double height)
{
int ix = Convert.ToInt32(x); //将指定的单精度浮点数的值转换为等价的32位有符号整数。
int iy = Convert.ToInt32(y);
int iw = Convert.ToInt32(width);
int ih = Convert.ToInt32(height);
//封装了GDI+位图,它由图形图像的像素数据和它的属性组成。一个系统绘图位图是一个用来处理像素数据定义的图像的对象。
System.Drawing.Bitmap bitmap = new Bitmap(iw, ih);
if (savePicture_flag == true)
{
using (System.Drawing.Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(0, 0, ix, iy, new System.Drawing.Size(iw, ih));
//对颜色数据进行一些块的传输,对像素的重新调整,从屏幕到系统绘图图形的绘图表面。
SaveFileDialog dialog = new SaveFileDialog(); //允许dialog来保存到一个地址
dialog.Filter = "Png Files|*.png"; // 文件保存类型
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) //将ok返回默认用户公共对话框
{
bitmap.Save(dialog.FileName); //保存到指定的文件
}
}
savePicture_flag = false;
}
}
}
}
XAML如下
<Window x:Class="CaptureWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CaptureWindow" Height="365.499" Width="525" AllowsTransparency="True" WindowStyle="None" Opacity="0.1" WindowState="Maximized" MouseDown="CaptureWindow_MouseDown" MouseMove="CaptureWindow_MouseMove" >
<Canvas x:Name="CaptureCanvas" HorizontalAlignment="Left" Height="316" VerticalAlignment="Top" Width="525" Background="White" Margin="0,10,0,0" >
<Button x:Name="savePicture" Content="保存图片" Canvas.Left="440" Canvas.Top="321" Width="75" Click="savePicture_Click" Background="red" FontWeight="Bold">
<Button.BindingGroup>
<BindingGroup/>
</Button.BindingGroup>
</Button>
</Canvas>
</Window>