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

@@ -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();

View File

@@ -55,9 +55,6 @@
<Compile Include="ScreenCaptureForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ScreenCaptureForm.Designer.cs">
<DependentUpon>ScreenCaptureForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="ResultForm.resx">
<DependentUpon>ResultForm.cs</DependentUpon>
</EmbeddedResource>
@@ -82,9 +79,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="ScreenCaptureForm.resx">
<DependentUpon>ScreenCaptureForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@@ -1,46 +0,0 @@
namespace ScreenCaptureApp
{
partial class ScreenCaptureForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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
}
}

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,
try
{
_screenBitmap = new Bitmap(
Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(screenBitmap))
using (var g = Graphics.FromImage(_screenBitmap))
{
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
g.CopyFromScreen(
Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
this.BackgroundImage = screenBitmap;
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,55 +98,65 @@ 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),
// Перекрестие для точного выбора
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(pen, new Point(rect.X + rect.Width / 2, rect.Y),
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));
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,
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)
{
@@ -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);
}
}
}

View File

@@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>