-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
162 lines (145 loc) · 5.92 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//FTP Snipping Tool
//By Darcy Johnson 2022
//
//Snipping Tool code by Eder A. Castro
//https://www.codeproject.com/Articles/485883/Create-your-own-Snipping-Tool
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace FTP_Snipping_Tool
{
public partial class Main : Form
{
//These variables control the mouse position
int selectX;
int selectY;
int selectWidth;
int selectHeight;
public Pen selectPen;
Bitmap _img;
//This variable control when you start the right click
bool start = false;
public static Image img = Properties.Resources.placeholder;
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
//Hide the Form
this.Hide();
//Create the Bitmap
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
//Create the Graphic Variable with screen Dimensions
Graphics graphics = Graphics.FromImage(printscreen as Image);
//Copy Image from the screen
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
//Create a temporal memory stream for the image
using (MemoryStream s = new MemoryStream())
{
//save graphic variable into memory
printscreen.Save(s, ImageFormat.Bmp);
pictureBox1.Size = new System.Drawing.Size(this.Width, this.Height);
//set the picture box with temporary stream
pictureBox1.Image = Image.FromStream(s);
}
//Show Form
this.Show();
//Cross Cursor
Cursor = Cursors.Cross;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//validate if there is an image
if (pictureBox1.Image == null)
return;
//validate if right-click was trigger
if (start)
{
//refresh picture box
pictureBox1.Refresh();
//set corner square to mouse coordinates
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
//draw dotted rectangle
pictureBox1.CreateGraphics().DrawRectangle(selectPen,
selectX, selectY, selectWidth, selectHeight);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//validate when user right-click
if (!start)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
//starts coordinates for rectangle
selectX = e.X;
selectY = e.Y;
selectPen = new Pen(Color.Crimson, 5);
selectPen.DashStyle = DashStyle.Dash;
}
//refresh picture box
pictureBox1.Refresh();
//start control variable for draw rectangle
start = true;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
//validate if there is image
if (pictureBox1.Image == null)
return;
//same functionality when mouse is over
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
pictureBox1.CreateGraphics().DrawRectangle(selectPen, selectX,
selectY, selectWidth, selectHeight);
}
start = false;
//function save image to clipboard
if (selectWidth == 0 || selectHeight == 0)
{
MessageBox.Show("Invalid parameter for screenshot area! Please try again.\nFor best results, drag from the top left to the bottom right.", "Screenshot Parameter Area Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SaveToClipboard();
}
private void SaveToClipboard()
{
//validate if something selected
if (selectWidth > 0)
{
Rectangle rect = new Rectangle(selectX, selectY, selectWidth, selectHeight);
//create bitmap with original dimensions
Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
//create bitmap with selected dimensions
try
{
_img = new Bitmap(selectWidth, selectHeight);
} catch
{
MessageBox.Show("Invalid parameter for screenshot area! Please try again.\nFor best results, drag from the top left to the bottom right.", "Screenshot Parameter Area Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//create graphic variable
Graphics g = Graphics.FromImage(_img);
//set graphic attributes
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);
//insert image stream into clipboard
Clipboard.SetImage(_img);
img = _img;
}
Form formsnipped = new snipped();
formsnipped.Show();
pictureBox1.Refresh();
this.Hide();
}
}
}