//記錄鼠標按下
public static bool MouseBtnIsDown = false;
//截圖起始座標
public static Point StartPoint;
//截圖的長寬
double width = 0;
double height = 0;spa
//鼠標按下事件
public void grid_mouseleftdown(object sender, MouseButtonEventArgs e)
{
//記錄按下操做
MouseBtnIsDown = true;
//記錄鼠標按下時的座標
Point p = e.GetPosition((IInputElement)sender);
StartPoint = p;
}
/// <summary>
/// 鼠標鬆開
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void grid_mouseleftup(object sender, MouseButtonEventArgs e)
{
//鼠標鬆開
MouseBtnIsDown = false;
//選中item(須要進行畫框或截圖的控件)
var item = TabControl.SelectedItem as TabItem;code
//獲得tabitem的content 爲Grid
Grid grid = item.Content as Grid;
foreach (var a in grid.Children)
{
if (a is Grid)
{
//畫框grid
var b = a as Grid;
b.IsHitTestVisible = false;
}
}orm
//獲取鼠標鬆開時的座標
Point Endpoint = e.GetPosition((IInputElement)sender);
//截圖事件
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)ShotGrid.ActualWidth, (int)ShotGrid.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
renderTargetBitmap.Render(ShotGrid);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
//控件截圖再截取想要的圖
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
//轉image
MemoryStream memoryStream = new MemoryStream();
pngImage.Save(memoryStream);
Image<Rgb, byte> m_image = new Image<Rgb, byte>(new System.Drawing.Bitmap(memoryStream));
m_image.ROI = new System.Drawing.Rectangle((int)StartPoint.X, (int)StartPoint.Y, (int)width, (int)height);
string path = @"C:\Users\Administrator\Desktop\截圖\";
Directory.CreateDirectory(path);
m_image.Save(@"C:\Users\Administrator\Desktop\截圖\" + DateTime.Now.Ticks + ".png");
Record("截圖保存成功" + @"C:\Users\Administrator\Desktop\截圖\" + DateTime.Now.Ticks + ".png", false);
memoryStream.Dispose();
m_image.Dispose();ci
}
//鼠標移動事件
public void grid_mousemove(object sender, System.Windows.Input.MouseEventArgs e)
{
//鼠標當前的point
Point Endpoint = e.GetPosition((IInputElement)sender);get
if (MouseBtnIsDown)
{
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
//轉換成基於Image控件的座標
width = Endpoint.X - StartPoint.X - 1;
height = Endpoint.Y - StartPoint.Y;
//用來記錄左上角座標
Point StartLeft = new Point();
//若是從左往右畫
if (Endpoint.X > StartPoint.X)
{
width = Endpoint.X - StartPoint.X;
StartLeft.X = StartPoint.X;
//若是從上往下畫
if (Endpoint.Y > StartPoint.Y)
{
height = Endpoint.Y - StartPoint.Y;
StartLeft.Y = StartPoint.Y;string
}
else
{
height = StartPoint.Y - Endpoint.Y;
StartLeft.Y = StartPoint.Y - height;
}
}
else
{
width = StartPoint.X - Endpoint.X;
StartLeft.X = Endpoint.X;
if (Endpoint.Y > StartPoint.Y)
{
height = Endpoint.Y - StartPoint.Y;
StartLeft.Y = Endpoint.Y - height;it
}
else
{
height = StartPoint.Y - Endpoint.Y;
StartLeft.Y = Endpoint.Y;
}
}
RectangleGeometry rec = new RectangleGeometry();
rec.Rect = new Rect(new System.Windows.Point(StartPoint.X, StartPoint.Y), new System.Windows.Size(width, height));
path.StrokeThickness = 2;
path.Data = rec;
path.StrokeDashArray = new DoubleCollection() { 2, 2 };
path.Stroke = System.Windows.Media.Brushes.WhiteSmoke;io
//清空全部畫出來的框
(sender as Grid).Children.Clear();
(sender as Grid).Opacity = 1;
(sender as Grid).Background = Brushes.Transparent;object
//添加方框
(sender as Grid).Children.Add(path);
//鼠標移動必定要設置穿透 否則鼠標擡起事件失靈
path.IsHitTestVisible = false;
rec.Freeze();
}
}