Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2025-06-09 20:35:03 +07:00
parent 322e9b4dbd
commit 09cee74f0c
5 changed files with 94 additions and 231 deletions

View File

@@ -5,23 +5,24 @@ using System.Windows.Forms;
namespace ScreenCaptureApp
{
public partial class ScreenCaptureForm : Form
public sealed class ScreenCaptureForm : Form
{
private Point startPoint;
private Point endPoint;
private bool isSelecting = false;
private Bitmap screenBitmap;
private Rectangle selectedRegion;
private Point _startPoint;
private Point _endPoint;
private bool _isSelecting;
private Bitmap _screenBitmap;
private Rectangle _selectedRegion;
private Pen _selectionPen;
public Image CapturedImage { get; private set; }
public Bitmap CapturedImage { get; private set; }
public ScreenCaptureForm()
{
InitializeComponent();
InitializeCaptureForm();
InitializeForm();
CaptureScreen();
}
private void InitializeCaptureForm()
private void InitializeForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
@@ -29,55 +30,67 @@ namespace ScreenCaptureApp
this.DoubleBuffered = true;
this.Cursor = Cursors.Cross;
this.KeyPreview = true;
this.StartPosition = FormStartPosition.Manual;
this.Location = Point.Empty;
this.Size = Screen.PrimaryScreen.Bounds.Size;
// Создаем скриншот всего экрана
CaptureScreen();
_selectionPen = new Pen(Color.Red, 2) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash };
}
private void CaptureScreen()
{
screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(screenBitmap))
try
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
_screenBitmap = new Bitmap(
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
this.BackgroundImage = screenBitmap;
using (var g = Graphics.FromImage(_screenBitmap))
{
g.CopyFromScreen(
Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
this.BackgroundImage = _screenBitmap;
}
catch
{
_screenBitmap?.Dispose();
throw;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
startPoint = e.Location;
isSelecting = true;
_startPoint = e.Location;
_isSelecting = true;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (isSelecting)
if (_isSelecting)
{
endPoint = e.Location;
this.Invalidate(); // Перерисовываем форму
_endPoint = e.Location;
this.Invalidate();
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && isSelecting)
if (e.Button == MouseButtons.Left && _isSelecting)
{
endPoint = e.Location;
isSelecting = false;
_endPoint = e.Location;
_isSelecting = false;
CaptureSelectedRegion();
}
base.OnMouseUp(e);
@@ -85,19 +98,18 @@ namespace ScreenCaptureApp
protected override void OnPaint(PaintEventArgs e)
{
if (isSelecting)
if (_isSelecting)
{
using (Pen pen = new Pen(Color.Red, 2))
{
Rectangle rect = GetSelectionRectangle();
e.Graphics.DrawRectangle(pen, rect);
var rect = GetSelectionRectangle();
e.Graphics.DrawRectangle(_selectionPen, rect);
// Рисуем перекрестие для точного выбора
e.Graphics.DrawLine(pen, new Point(rect.X, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width, rect.Y + rect.Height / 2));
e.Graphics.DrawLine(pen, new Point(rect.X + rect.Width / 2, rect.Y),
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height));
}
// Перекрестие для точного выбора
e.Graphics.DrawLine(_selectionPen,
new Point(rect.X, rect.Y + rect.Height / 2),
new Point(rect.X + rect.Width, rect.Y + rect.Height / 2));
e.Graphics.DrawLine(_selectionPen,
new Point(rect.X + rect.Width / 2, rect.Y),
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height));
}
base.OnPaint(e);
}
@@ -105,34 +117,45 @@ namespace ScreenCaptureApp
private Rectangle GetSelectionRectangle()
{
return new Rectangle(
Math.Min(startPoint.X, endPoint.X),
Math.Min(startPoint.Y, endPoint.Y),
Math.Abs(startPoint.X - endPoint.X),
Math.Abs(startPoint.Y - endPoint.Y));
Math.Min(_startPoint.X, _endPoint.X),
Math.Min(_startPoint.Y, _endPoint.Y),
Math.Abs(_startPoint.X - _endPoint.X),
Math.Abs(_startPoint.Y - _endPoint.Y));
}
private void CaptureSelectedRegion()
{
selectedRegion = GetSelectionRectangle();
_selectedRegion = GetSelectionRectangle();
if (selectedRegion.Width <= 0 || selectedRegion.Height <= 0)
if (_selectedRegion.Width <= 0 || _selectedRegion.Height <= 0)
{
this.DialogResult = DialogResult.Cancel;
return;
}
// Вырезаем выбранную область
CapturedImage = new Bitmap(selectedRegion.Width, selectedRegion.Height);
using (Graphics g = Graphics.FromImage(CapturedImage))
try
{
g.DrawImage(screenBitmap, new Rectangle(0, 0, selectedRegion.Width, selectedRegion.Height),
selectedRegion,
GraphicsUnit.Pixel);
}
CapturedImage = new Bitmap(_selectedRegion.Width, _selectedRegion.Height);
this.DialogResult = DialogResult.OK;
this.Close();
using (var g = Graphics.FromImage(CapturedImage))
{
g.DrawImage(_screenBitmap,
new Rectangle(0, 0, _selectedRegion.Width, _selectedRegion.Height),
_selectedRegion,
GraphicsUnit.Pixel);
}
this.DialogResult = DialogResult.OK;
}
catch
{
CapturedImage?.Dispose();
this.DialogResult = DialogResult.Cancel;
}
finally
{
this.Close();
}
}
protected override void OnKeyDown(KeyEventArgs e)
@@ -144,5 +167,16 @@ namespace ScreenCaptureApp
}
base.OnKeyDown(e);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_screenBitmap?.Dispose();
_selectionPen?.Dispose();
CapturedImage?.Dispose();
}
base.Dispose(disposing);
}
}
}