diff --git a/ResultForm.Designer.cs b/ResultForm.Designer.cs index ca599ca..95a1716 100644 --- a/ResultForm.Designer.cs +++ b/ResultForm.Designer.cs @@ -83,6 +83,7 @@ this.Controls.Add(this.btnCopy); this.Controls.Add(this.btnSave); this.Controls.Add(this.pictureBox1); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "ResultForm"; this.Text = "ResultForm"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); diff --git a/ScreenCaptureApp.csproj b/ScreenCaptureApp.csproj index 1b83899..1adcf37 100644 --- a/ScreenCaptureApp.csproj +++ b/ScreenCaptureApp.csproj @@ -55,9 +55,6 @@ Form - - ScreenCaptureForm.cs - ResultForm.cs @@ -82,9 +79,6 @@ Resources.resx True - - ScreenCaptureForm.cs - SettingsSingleFileGenerator Settings.Designer.cs diff --git a/ScreenCaptureForm.Designer.cs b/ScreenCaptureForm.Designer.cs deleted file mode 100644 index 81dc459..0000000 --- a/ScreenCaptureForm.Designer.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace ScreenCaptureApp -{ - partial class ScreenCaptureForm - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.SuspendLayout(); - // - // ScreenCaptureForm - // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(430, 167); - this.Name = "ScreenCaptureForm"; - this.Text = "ScreenCaptureForm"; - this.ResumeLayout(false); - - } - - #endregion - } -} \ No newline at end of file diff --git a/ScreenCaptureForm.cs b/ScreenCaptureForm.cs index b7e00c7..36b80d6 100644 --- a/ScreenCaptureForm.cs +++ b/ScreenCaptureForm.cs @@ -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); + } } } \ No newline at end of file diff --git a/ScreenCaptureForm.resx b/ScreenCaptureForm.resx deleted file mode 100644 index 1af7de1..0000000 --- a/ScreenCaptureForm.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file