148 lines
4.7 KiB
C#
148 lines
4.7 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ScreenCaptureApp
|
|
{
|
|
public partial class ScreenCaptureForm : Form
|
|
{
|
|
private Point startPoint;
|
|
private Point endPoint;
|
|
private bool isSelecting = false;
|
|
private Bitmap screenBitmap;
|
|
private Rectangle selectedRegion;
|
|
|
|
public Image CapturedImage { get; private set; }
|
|
|
|
public ScreenCaptureForm()
|
|
{
|
|
InitializeComponent();
|
|
InitializeCaptureForm();
|
|
}
|
|
|
|
private void InitializeCaptureForm()
|
|
{
|
|
this.FormBorderStyle = FormBorderStyle.None;
|
|
this.WindowState = FormWindowState.Maximized;
|
|
this.Opacity = 0.3;
|
|
this.DoubleBuffered = true;
|
|
this.Cursor = Cursors.Cross;
|
|
this.KeyPreview = true;
|
|
|
|
// Создаем скриншот всего экрана
|
|
CaptureScreen();
|
|
}
|
|
|
|
private void CaptureScreen()
|
|
{
|
|
screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
|
|
Screen.PrimaryScreen.Bounds.Height,
|
|
PixelFormat.Format32bppArgb);
|
|
|
|
using (Graphics 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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
using (Pen pen = new Pen(Color.Red, 2))
|
|
{
|
|
Rectangle rect = GetSelectionRectangle();
|
|
e.Graphics.DrawRectangle(pen, 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));
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
// Вырезаем выбранную область
|
|
CapturedImage = new Bitmap(selectedRegion.Width, selectedRegion.Height);
|
|
|
|
using (Graphics g = Graphics.FromImage(CapturedImage))
|
|
{
|
|
g.DrawImage(screenBitmap, new Rectangle(0, 0, selectedRegion.Width, selectedRegion.Height),
|
|
selectedRegion,
|
|
GraphicsUnit.Pixel);
|
|
}
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
this.Close();
|
|
}
|
|
|
|
protected override void OnKeyDown(KeyEventArgs e)
|
|
{
|
|
if (e.KeyCode == Keys.Escape)
|
|
{
|
|
this.DialogResult = DialogResult.Cancel;
|
|
this.Close();
|
|
}
|
|
base.OnKeyDown(e);
|
|
}
|
|
}
|
|
} |