182 lines
5.4 KiB
C#
182 lines
5.4 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ScreenCaptureApp
|
|
{
|
|
public sealed class ScreenCaptureForm : Form
|
|
{
|
|
private Point _startPoint;
|
|
private Point _endPoint;
|
|
private bool _isSelecting;
|
|
private Bitmap _screenBitmap;
|
|
private Rectangle _selectedRegion;
|
|
private Pen _selectionPen;
|
|
|
|
public Bitmap CapturedImage { get; private set; }
|
|
|
|
public ScreenCaptureForm()
|
|
{
|
|
InitializeForm();
|
|
CaptureScreen();
|
|
}
|
|
|
|
private void InitializeForm()
|
|
{
|
|
this.FormBorderStyle = FormBorderStyle.None;
|
|
this.WindowState = FormWindowState.Maximized;
|
|
this.Opacity = 0.3;
|
|
this.DoubleBuffered = true;
|
|
this.Cursor = Cursors.Cross;
|
|
this.KeyPreview = true;
|
|
this.StartPosition = FormStartPosition.Manual;
|
|
this.Location = Point.Empty;
|
|
this.Size = Screen.PrimaryScreen.Bounds.Size;
|
|
|
|
_selectionPen = new Pen(Color.Red, 2) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash };
|
|
}
|
|
|
|
private void CaptureScreen()
|
|
{
|
|
try
|
|
{
|
|
_screenBitmap = new Bitmap(
|
|
Screen.PrimaryScreen.Bounds.Width,
|
|
Screen.PrimaryScreen.Bounds.Height,
|
|
PixelFormat.Format32bppArgb);
|
|
|
|
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;
|
|
}
|
|
base.OnMouseDown(e);
|
|
}
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e)
|
|
{
|
|
if (_isSelecting)
|
|
{
|
|
_endPoint = e.Location;
|
|
this.Invalidate();
|
|
}
|
|
base.OnMouseMove(e);
|
|
}
|
|
|
|
protected override void OnMouseUp(MouseEventArgs e)
|
|
{
|
|
if (e.Button == MouseButtons.Left && _isSelecting)
|
|
{
|
|
_endPoint = e.Location;
|
|
_isSelecting = false;
|
|
CaptureSelectedRegion();
|
|
}
|
|
base.OnMouseUp(e);
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
if (_isSelecting)
|
|
{
|
|
var rect = GetSelectionRectangle();
|
|
e.Graphics.DrawRectangle(_selectionPen, rect);
|
|
|
|
// Перекрестие для точного выбора
|
|
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);
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
private void CaptureSelectedRegion()
|
|
{
|
|
_selectedRegion = GetSelectionRectangle();
|
|
|
|
if (_selectedRegion.Width <= 0 || _selectedRegion.Height <= 0)
|
|
{
|
|
this.DialogResult = DialogResult.Cancel;
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
CapturedImage = new Bitmap(_selectedRegion.Width, _selectedRegion.Height);
|
|
|
|
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)
|
|
{
|
|
if (e.KeyCode == Keys.Escape)
|
|
{
|
|
this.DialogResult = DialogResult.Cancel;
|
|
this.Close();
|
|
}
|
|
base.OnKeyDown(e);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_screenBitmap?.Dispose();
|
|
_selectionPen?.Dispose();
|
|
CapturedImage?.Dispose();
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
} |