Skip to content

Commit

Permalink
publish
Browse files Browse the repository at this point in the history
  • Loading branch information
pcinfogmach committed Nov 3, 2024
1 parent ecbe3ed commit 4e89357
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
3 changes: 2 additions & 1 deletion ScreenCapture/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
AllowsTransparency="True" WindowStyle="None"
PreviewKeyDown="Window_PreviewKeyDown">
<Window.Background>
<SolidColorBrush Opacity="0.1" Color="WhiteSmoke"/>
<SolidColorBrush Opacity="0"/>
</Window.Background>
<Canvas x:Name="canvas" Background="Transparent"
MouseDown="canvas_MouseDown"
MouseUp="canvas_MouseUp">
<Rectangle Name="Overlay" Fill="LightGray" Opacity="0.3"/>
</Canvas>
</Window>
72 changes: 50 additions & 22 deletions ScreenCapture/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ScreenCapture
{
public partial class MainWindow : Window
{
private System.Windows.Shapes.Rectangle DragRectangle = null;
private System.Windows.Point StartPoint, LastPoint;
private System.Windows.Shapes.Rectangle dashedRectangle;
bool isClosed;

public MainWindow()
{
InitializeComponent();
this.Loaded += (s, e) =>
{
Overlay.Width = this.ActualWidth;
Overlay.Height = this.ActualHeight;
};
Cursor = Cursors.Cross;
}

Expand All @@ -29,50 +36,70 @@ private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
StartPoint = Mouse.GetPosition(canvas);
LastPoint = StartPoint;

DragRectangle = new System.Windows.Shapes.Rectangle
{
Width = 1,
Height = 1,
Stroke = System.Windows.Media.Brushes.Red,
StrokeThickness = 1,
Cursor = Cursors.Cross
};

canvas.Children.Add(DragRectangle);
Canvas.SetLeft(DragRectangle, StartPoint.X);
Canvas.SetTop(DragRectangle, StartPoint.Y);

// Start tracking mouse move and mouse up events
canvas.MouseMove += canvas_MouseMove;
canvas.MouseUp += canvas_MouseUp;
canvas.CaptureMouse();
}

private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (DragRectangle == null) return;
if (StartPoint == null) return;

LastPoint = Mouse.GetPosition(canvas);

DragRectangle.Width = Math.Abs(LastPoint.X - StartPoint.X);
DragRectangle.Height = Math.Abs(LastPoint.Y - StartPoint.Y);
Canvas.SetLeft(DragRectangle, Math.Min(LastPoint.X, StartPoint.X));
Canvas.SetTop(DragRectangle, Math.Min(LastPoint.Y, StartPoint.Y));
// Calculate the selected area
double x = Math.Min(LastPoint.X, StartPoint.X);
double y = Math.Min(LastPoint.Y, StartPoint.Y);
double width = Math.Abs(LastPoint.X - StartPoint.X);
double height = Math.Abs(LastPoint.Y - StartPoint.Y);

// Create a combined geometry to clip out the selected rectangle area from the overlay
RectangleGeometry fullArea = new RectangleGeometry(new Rect(0, 0, canvas.ActualWidth, canvas.ActualHeight));
RectangleGeometry selectionArea = new RectangleGeometry(new Rect(x, y, width, height));
CombinedGeometry combinedGeometry = new CombinedGeometry(GeometryCombineMode.Exclude, fullArea, selectionArea);

// Apply the clipping to the overlay rectangle
Overlay.Clip = combinedGeometry;

// Create or update the dashed rectangle

if (dashedRectangle == null)
{
dashedRectangle = new System.Windows.Shapes.Rectangle
{
Stroke = System.Windows.Media.Brushes.Red, // Set the outline color
StrokeDashArray = new DoubleCollection { 4, 2 }, // Dash pattern
StrokeThickness = 2 // Set outline thickness
};
canvas.Children.Add(dashedRectangle); // Add it to the canvas
}

// Set the position and size of the dashed rectangle
Canvas.SetLeft(dashedRectangle, x);
Canvas.SetTop(dashedRectangle, y);
dashedRectangle.Width = width;
dashedRectangle.Height = height;
}


private void canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
if (DragRectangle == null) return;
canvas.ReleaseMouseCapture();
canvas.MouseMove -= canvas_MouseMove;
canvas.MouseUp -= canvas_MouseUp;

CaptureScreen();

canvas.Children.Remove(DragRectangle);
DragRectangle = null;
// Reset the overlay clip and remove selection
Overlay.Clip = null;
this.Close();
}


private void CaptureScreen()
{
if (isClosed) return;
var screenStart = PointToScreen(StartPoint);
var screenEnd = PointToScreen(LastPoint);

Expand All @@ -83,6 +110,7 @@ private void CaptureScreen()

this.Closed += (s, e) =>
{
isClosed = true;
if (width > 0 && height > 0)
{
using (Bitmap bitmap = new Bitmap(width, height))
Expand Down
2 changes: 1 addition & 1 deletion ScreenCapture/PreviewWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<Button x:Name="SaveTextButton" Content="Save Text" Click="SaveTextButton_Click"/>
<Button x:Name="CopyImageButton" Content="Copy Image" Click="CopyImageButton_Click"/>
<Button x:Name="CopyTextButton" Content="Copy Text" Click="CopyTextButton_Click"/>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click"/>
<Button x:Name="RestartButton" Content="New Capture" Click="RestartButton_Click"/>

</StackPanel>
</Grid>
Expand Down
12 changes: 10 additions & 2 deletions ScreenCapture/PreviewWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class PreviewWindow : Window
{
private readonly BitmapImage _bitmapImage;
private readonly MemoryStream _imageStream;
bool isNewCaptureStart;

public PreviewWindow(BitmapImage bitmapImage, MemoryStream imageStream)
{
Expand All @@ -24,7 +25,7 @@ public PreviewWindow(BitmapImage bitmapImage, MemoryStream imageStream)
_imageStream = imageStream;
PreviewImage.Source = _bitmapImage;
ExtractTextFromImage();
this.Closed += (s, e) => { App.Current.Shutdown(); };
this.Closed += (s, e) => { if (!isNewCaptureStart) App.Current.Shutdown(); };
}

private void ApplyTheme()
Expand Down Expand Up @@ -66,7 +67,7 @@ private void SetButtonContentBasedOnLanguage()
SaveTextButton.Content = "שמור טקסט";
CopyImageButton.Content = "העתק תמונה";
CopyTextButton.Content = "העתק טקסט";
CancelButton.Content = "ביטול";
RestartButton.Content = "לכידה חדשה";
}
}

Expand Down Expand Up @@ -155,5 +156,12 @@ private void CancelButton_Click(object sender, RoutedEventArgs e)
{
Close();
}

private void RestartButton_Click(object sender, RoutedEventArgs e)
{
isNewCaptureStart = true;
this.Close();
new MainWindow().ShowDialog();
}
}
}

0 comments on commit 4e89357

Please sign in to comment.