This repository was archived by the owner on Aug 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathfrmImageDialog.cs
More file actions
203 lines (160 loc) · 6.31 KB
/
frmImageDialog.cs
File metadata and controls
203 lines (160 loc) · 6.31 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Technitium Bit Chat
Copyright (C) 2016 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace BitChatApp
{
public partial class frmImageDialog : Form
{
#region variables
Pen cropPen = new Pen(Color.White, 2) { DashStyle = DashStyle.Dash };
Rectangle cropRectangle;
Image selectedImage;
#endregion
#region constructor
public frmImageDialog()
{
InitializeComponent();
}
#endregion
#region form code
private void frmImageDialog_Load(object sender, EventArgs e)
{
btnBrowse_Click(null, null);
}
private static Image CropImage(PictureBox box, Rectangle cropRectangle)
{
//find virtual box size and fix crop rectangle X & Y
double imageRatio = box.Image.Width / (double)box.Image.Height;
double boxRatio = box.Width / (double)box.Height;
int vBoxWidth;
int vBoxHeight;
Rectangle vCropRectangle = cropRectangle;
if (imageRatio > boxRatio)
{
//box height is more
vBoxWidth = box.Width;
vBoxHeight = (int)(box.Width / imageRatio);
int padding = (box.Height - vBoxHeight) / 2;
vCropRectangle.Y -= padding;
}
else
{
//box width is more
vBoxWidth = (int)(box.Height * imageRatio);
vBoxHeight = box.Height;
int padding = (box.Width - vBoxWidth) / 2;
vCropRectangle.X -= padding;
}
//scale crop rectangle
double widthRatio = box.Image.Width / (double)vBoxWidth;
double heightRatio = box.Image.Height / (double)vBoxHeight;
vCropRectangle.X = (int)(vCropRectangle.X * widthRatio);
vCropRectangle.Y = (int)(vCropRectangle.Y * heightRatio);
vCropRectangle.Width = (int)(vCropRectangle.Width * widthRatio);
vCropRectangle.Height = (int)(vCropRectangle.Height * heightRatio);
return CropImage(box.Image, vCropRectangle);
}
private static Image CropImage(Image original, Rectangle cropRectangle)
{
Bitmap image = new Bitmap(cropRectangle.Width, cropRectangle.Height);
image.SetResolution(original.HorizontalResolution, original.VerticalResolution);
Graphics g = Graphics.FromImage(image);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(original, 0, 0, cropRectangle, GraphicsUnit.Pixel);
return image;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog oFD = new OpenFileDialog())
{
if (oFD.ShowDialog(this) == DialogResult.OK)
{
oFD.CheckFileExists = true;
oFD.CheckPathExists = true;
oFD.Multiselect = false;
oFD.Title = "Select Image";
try
{
using (FileStream fS = new FileStream(oFD.FileName, FileMode.Open, FileAccess.Read))
{
picImage.Image = Image.FromStream(fS);
}
}
catch (Exception ex)
{
MessageBox.Show("Error occured while opening the selected file:\n\n" + ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void picImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
cropRectangle.X = e.X;
cropRectangle.Y = e.Y;
cropRectangle.Width = 0;
cropRectangle.Height = 0;
picImage.Refresh();
}
}
private void picImage_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (picImage.Image != null)
{
picImage.Refresh();
cropRectangle.Width = e.X - cropRectangle.X;
cropRectangle.Height = e.Y - cropRectangle.Y;
if (cropRectangle.Width > cropRectangle.Height)
cropRectangle.Height = cropRectangle.Width;
else
cropRectangle.Width = cropRectangle.Height;
picImage.CreateGraphics().DrawRectangle(cropPen, cropRectangle);
}
}
}
private void picImage_MouseUp(object sender, MouseEventArgs e)
{
btnOK.Enabled = (cropRectangle.Width > 0);
}
private void btnOK_Click(object sender, EventArgs e)
{
using (Image croppedImage = CropImage(picImage, cropRectangle))
{
selectedImage = new Bitmap(croppedImage, 256, 256);
}
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region properties
public Image SelectedImage
{ get { return selectedImage; } }
#endregion
}
}