This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
144 lines (126 loc) · 4.49 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Picscut
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
PicsList.Init();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
Size FitRatio(Size container, Size target)
{
Size sw = new Size(container.Width, target.Height * container.Width / target.Width);
Size sh = new Size(target.Width * container.Height / target.Height, container.Height);
if (sw.Height < container.Height) return sw;
else return sh;
}
private void PicsList_SelectedIndexChanged(object sender, EventArgs e)
{
Cropper.Image = PicsList.SelectedPicture.Bitmap;
AdjustCropperSize();
}
private void AdjustCropperSize()
{
if (Cropper.Image == null)
return;
Cropper.Size = FitRatio(PicViewer.Size, Cropper.Image.Size);
Cropper.Left = (PicViewer.Width - Cropper.Width) / 2;
Cropper.Top = (PicViewer.Height - Cropper.Height) / 2;
Cropper.EffectizeSize = Cropper.Image.Size;
Cropper.Invalidate();
}
private void PicViewer_Resize(object sender, EventArgs e)
{
AdjustCropperSize();
}
private void PicsList_PicsSizeChanged(object sender, EventArgs e)
{
}
bool boundsUpdating = false;
private void Cropper_SelectionBoundsChanged(object arg1, EventArgs arg2)
{
if (boundsUpdating)
return;
boundsUpdating = true;
SelTopBox.Value = Cropper.SelectionBounds.Top;
SelLeftBox.Value = Cropper.SelectionBounds.Left;
SelWidthBox.Value = Cropper.SelectionBounds.Width;
SelHeightBox.Value = Cropper.SelectionBounds.Height;
boundsUpdating = false;
}
int clamp(int x, int a, int b)
{
if (x < a) return a;
if (x > b) return b;
return x;
}
private void SelBox_ValueChanged(object sender, EventArgs e)
{
if (boundsUpdating)
return;
boundsUpdating = true;
int left = clamp((int)SelLeftBox.Value, 0, Cropper.Image.Width);
int top = clamp((int)SelTopBox.Value, 0, Cropper.Image.Height);
int width = (int)SelWidthBox.Value;
int height = (int)SelHeightBox.Value;
Cropper.SelectionBounds = new Rectangle(left, top, width, height);
boundsUpdating = false;
}
private void SelBox_KeyUp(object sender, KeyEventArgs e)
{
// Trigger ValueChanged while typing
var v = (sender as NumericUpDown).Value;
}
private void SaveOverBtn_Click(object sender, EventArgs e)
{
foreach(var pic in PicsList.Pictures)
{
var path = pic.Path;
var dir = Path.Combine(Path.GetDirectoryName(path), "cropped");
path = Path.Combine(dir, Path.GetFileName(path));
Directory.CreateDirectory(dir);
pic.Crop(Cropper.SelectionBounds).Save(path);
}
}
private void PicsList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void PicsList_DragDrop(object sender, DragEventArgs e)
{
bool wasEmpty = PicsList.Pictures.Count == 0;
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach(var file in files)
{
try
{
PicsList.AddPicture(file);
}
catch(Exception ex)
{
MessageBox.Show($"Could not add file {file}\n\nReason: {ex.Message}", "Error");
}
}
if(wasEmpty) // so no selection was active
{
PicsList.SetSelected(0, true);
if (!Cropper.Visible)
Cropper.Show();
}
}
}
}