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.btnCopy);
this.Controls.Add(this.btnSave); this.Controls.Add(this.btnSave);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "ResultForm"; this.Name = "ResultForm";
this.Text = "ResultForm"; this.Text = "ResultForm";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();

View File

@@ -55,9 +55,6 @@
<Compile Include="ScreenCaptureForm.cs"> <Compile Include="ScreenCaptureForm.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
<Compile Include="ScreenCaptureForm.Designer.cs">
<DependentUpon>ScreenCaptureForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="ResultForm.resx"> <EmbeddedResource Include="ResultForm.resx">
<DependentUpon>ResultForm.cs</DependentUpon> <DependentUpon>ResultForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
@@ -82,9 +79,6 @@
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
</Compile> </Compile>
<EmbeddedResource Include="ScreenCaptureForm.resx">
<DependentUpon>ScreenCaptureForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="Properties\Settings.settings"> <None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <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 namespace ScreenCaptureApp
{ {
public partial class ScreenCaptureForm : Form public sealed class ScreenCaptureForm : Form
{ {
private Point startPoint; private Point _startPoint;
private Point endPoint; private Point _endPoint;
private bool isSelecting = false; private bool _isSelecting;
private Bitmap screenBitmap; private Bitmap _screenBitmap;
private Rectangle selectedRegion; private Rectangle _selectedRegion;
private Pen _selectionPen;
public Image CapturedImage { get; private set; } public Bitmap CapturedImage { get; private set; }
public ScreenCaptureForm() public ScreenCaptureForm()
{ {
InitializeComponent(); InitializeForm();
InitializeCaptureForm(); CaptureScreen();
} }
private void InitializeCaptureForm() private void InitializeForm()
{ {
this.FormBorderStyle = FormBorderStyle.None; this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized; this.WindowState = FormWindowState.Maximized;
@@ -29,55 +30,67 @@ namespace ScreenCaptureApp
this.DoubleBuffered = true; this.DoubleBuffered = true;
this.Cursor = Cursors.Cross; this.Cursor = Cursors.Cross;
this.KeyPreview = true; 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 };
CaptureScreen();
} }
private void CaptureScreen() private void CaptureScreen()
{ {
screenBitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, try
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(screenBitmap))
{ {
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, _screenBitmap = new Bitmap(
Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Width,
0, 0, Screen.PrimaryScreen.Bounds.Height,
Screen.PrimaryScreen.Bounds.Size, PixelFormat.Format32bppArgb);
CopyPixelOperation.SourceCopy);
}
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) protected override void OnMouseDown(MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
{ {
startPoint = e.Location; _startPoint = e.Location;
isSelecting = true; _isSelecting = true;
} }
base.OnMouseDown(e); base.OnMouseDown(e);
} }
protected override void OnMouseMove(MouseEventArgs e) protected override void OnMouseMove(MouseEventArgs e)
{ {
if (isSelecting) if (_isSelecting)
{ {
endPoint = e.Location; _endPoint = e.Location;
this.Invalidate(); // Перерисовываем форму this.Invalidate();
} }
base.OnMouseMove(e); base.OnMouseMove(e);
} }
protected override void OnMouseUp(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left && isSelecting) if (e.Button == MouseButtons.Left && _isSelecting)
{ {
endPoint = e.Location; _endPoint = e.Location;
isSelecting = false; _isSelecting = false;
CaptureSelectedRegion(); CaptureSelectedRegion();
} }
base.OnMouseUp(e); base.OnMouseUp(e);
@@ -85,19 +98,18 @@ namespace ScreenCaptureApp
protected override void OnPaint(PaintEventArgs e) protected override void OnPaint(PaintEventArgs e)
{ {
if (isSelecting) if (_isSelecting)
{ {
using (Pen pen = new Pen(Color.Red, 2)) var rect = GetSelectionRectangle();
{ e.Graphics.DrawRectangle(_selectionPen, rect);
Rectangle rect = GetSelectionRectangle();
e.Graphics.DrawRectangle(pen, rect);
// Рисуем перекрестие для точного выбора // Перекрестие для точного выбора
e.Graphics.DrawLine(pen, new Point(rect.X, rect.Y + rect.Height / 2), e.Graphics.DrawLine(_selectionPen,
new Point(rect.X + rect.Width, rect.Y + rect.Height / 2)); new Point(rect.X, rect.Y + rect.Height / 2),
e.Graphics.DrawLine(pen, new Point(rect.X + rect.Width / 2, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height / 2));
new Point(rect.X + rect.Width / 2, rect.Y + rect.Height)); 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); base.OnPaint(e);
} }
@@ -105,34 +117,45 @@ namespace ScreenCaptureApp
private Rectangle GetSelectionRectangle() private Rectangle GetSelectionRectangle()
{ {
return new Rectangle( return new Rectangle(
Math.Min(startPoint.X, endPoint.X), Math.Min(_startPoint.X, _endPoint.X),
Math.Min(startPoint.Y, endPoint.Y), Math.Min(_startPoint.Y, _endPoint.Y),
Math.Abs(startPoint.X - endPoint.X), Math.Abs(_startPoint.X - _endPoint.X),
Math.Abs(startPoint.Y - endPoint.Y)); Math.Abs(_startPoint.Y - _endPoint.Y));
} }
private void CaptureSelectedRegion() 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; this.DialogResult = DialogResult.Cancel;
return; return;
} }
// Вырезаем выбранную область try
CapturedImage = new Bitmap(selectedRegion.Width, selectedRegion.Height);
using (Graphics g = Graphics.FromImage(CapturedImage))
{ {
g.DrawImage(screenBitmap, new Rectangle(0, 0, selectedRegion.Width, selectedRegion.Height), CapturedImage = new Bitmap(_selectedRegion.Width, _selectedRegion.Height);
selectedRegion,
GraphicsUnit.Pixel);
}
this.DialogResult = DialogResult.OK; using (var g = Graphics.FromImage(CapturedImage))
this.Close(); {
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) protected override void OnKeyDown(KeyEventArgs e)
@@ -144,5 +167,16 @@ namespace ScreenCaptureApp
} }
base.OnKeyDown(e); 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>