//记录鼠标按下
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();
}
}
Wpf 自定义截图的实现吴佳、 四海商舟、 法瑞儿、 亚马逊整顿VC,下一步亚马逊的"规则"将如何制定?、 一波大红利!电商巨头沃尔玛对中国卖家开放!、 惊呆!1个月内亚马逊美国站收到100万条差评、 水乡寻梦 绍兴四日深度游自助攻略、 唇舌间的爱恋 从波尔多到卢瓦河谷(组图)、 十一游无锡行程小记_无锡攻略、
No comments:
Post a Comment