Implementation of WPF custom screenshot

Time:2020-9-8

//Record mouse press
public static bool MouseBtnIsDown = false;
//Starting coordinates of Screenshot
public static Point StartPoint;
//The length and width of the screenshot
double width = 0;
double height = 0;

//Mouse down events
public void grid_mouseleftdown(object sender, MouseButtonEventArgs e)
{
//Record press action
MouseBtnIsDown = true;
//Record the coordinates when the mouse is pressed
Point p = e.GetPosition((IInputElement)sender);
StartPoint = p;
}
///

///Mouse release
///

///
///
public void grid_mouseleftup(object sender, MouseButtonEventArgs e)
{
//Mouse release
MouseBtnIsDown = false;
//Check item (the control that needs a frame or screenshot)
var item = TabControl.SelectedItem as TabItem;

//The content of tabitem is grid
Grid grid = item.Content as Grid;
foreach (var a in grid.Children)
{
if (a is Grid)
{
//Frame grid
var b = a as Grid;
b.IsHitTestVisible = false;
}
}

//Gets the coordinates when the mouse is released
Point Endpoint = e.GetPosition((IInputElement)sender);
//Screenshot

RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)ShotGrid.ActualWidth, (int)ShotGrid.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
renderTargetBitmap.Render(ShotGrid);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
//Control and then capture the desired image
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
//To image
MemoryStream memoryStream = new MemoryStream();
pngImage.Save(memoryStream);
Image m_image = new Image(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 / screenshot \”;
Directory.CreateDirectory(path);
M_ image.Save (@ “C: users / administrator / desktop / screenshot \”+ DateTime.Now.Ticks + “.png”);
Record (“screenshot saved successfully” + @ “C: users / administrator / desktop / screenshot \”+ DateTime.Now.Ticks + “.png”, false);
memoryStream.Dispose();
m_image.Dispose();

}
//Mouse movement events
public void grid_mousemove(object sender, System.Windows.Input.MouseEventArgs e)
{
//The current point of the mouse
Point Endpoint = e.GetPosition((IInputElement)sender);

if (MouseBtnIsDown)
{
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
//Convert to coordinates based on image control
width = Endpoint.X – StartPoint.X – 1;
height = Endpoint.Y – StartPoint.Y;
//Used to record the coordinates of the upper left corner
Point StartLeft = new Point();
//If you draw from left to right
if (Endpoint.X > StartPoint.X)
{
width = Endpoint.X – StartPoint.X;
StartLeft.X = StartPoint.X;
//If you draw from the top down
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;

//Empty all drawn boxes
(sender as Grid).Children.Clear();
(sender as Grid).Opacity = 1;
(sender as Grid).Background = Brushes.Transparent;

//Add box
(sender as Grid).Children.Add(path);
//Mouse movement must be set penetration, otherwise mouse lift event failure
path.IsHitTestVisible = false;
rec.Freeze();
}

}