//記錄滑鼠按下 public static bool MouseBtnIsDown = false; //截圖起始坐標 public static Point StartPoint; //截圖的長寬 double width = 0; double height = 0; //滑鼠按下事件 pub ...
//記錄滑鼠按下
public static bool MouseBtnIsDown = false;
//截圖起始坐標
public static Point StartPoint;
//截圖的長寬
double width = 0;
double height = 0;
//滑鼠按下事件
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;
//得到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;
}
}
//獲取滑鼠鬆開時的坐標
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();
}
//滑鼠移動事件
public void grid_mousemove(object sender, System.Windows.Input.MouseEventArgs e)
{
//滑鼠當前的point
Point Endpoint = e.GetPosition((IInputElement)sender);
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;
}
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;
}
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;
//清空所有畫出來的框
(sender as Grid).Children.Clear();
(sender as Grid).Opacity = 1;
(sender as Grid).Background = Brushes.Transparent;
//添加方框
(sender as Grid).Children.Add(path);
//滑鼠移動一定要設置穿透 不然滑鼠抬起事件失靈
path.IsHitTestVisible = false;
rec.Freeze();
}
}