-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImageIsolatorMain.pas
352 lines (318 loc) · 8.14 KB
/
ImageIsolatorMain.pas
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
unit ImageIsolatorMain;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,
ClipBrd, ExtCtrls, Buttons, ExtDlgs, Menus, StdCtrls, ComCtrls,
ToolWin;
const
DelayTime = 2000;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
mnuOptions: TMenuItem;
OptionsCapturing: TMenuItem;
OptionBGColor: TMenuItem;
ColorDialog1: TColorDialog;
OpenPictureDialog1: TOpenPictureDialog;
SavePictureDialog1: TSavePictureDialog;
Panel2: TPanel;
BGColor: TShape;
Label1: TLabel;
CurrColor: TShape;
Label2: TLabel;
ScrollBox1: TScrollBox;
Image1: TImage;
ActionsMenu: TMenuItem;
ActionIsolate: TMenuItem;
ActionAlign: TMenuItem;
ActionTile: TMenuItem;
EditMenu: TMenuItem;
EditUndo: TMenuItem;
EditRedo: TMenuItem;
EditCut: TMenuItem;
EditCopy: TMenuItem;
EditPaste: TMenuItem;
EditCopyToFile: TMenuItem;
EditPasteFromFile: TMenuItem;
EditCapture: TMenuItem;
procedure OptionBGColorClick(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ActionIsolateClick(Sender: TObject);
procedure ActionAlignClick(Sender: TObject);
procedure ActionTileClick(Sender: TObject);
procedure EditUndoClick(Sender: TObject);
procedure EditRedoClick(Sender: TObject);
procedure EditCutClick(Sender: TObject);
procedure EditCopyClick(Sender: TObject);
procedure EditCopyToFileClick(Sender: TObject);
procedure EditPasteClick(Sender: TObject);
procedure EditPasteFromFileClick(Sender: TObject);
procedure EditCaptureClick(Sender: TObject);
private
procedure AssignBitmap(b: TBitmap);
procedure AssignPicture(b: TPicture);
procedure CaptureArea;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses frmAreaCapture, ScrnCap, CanvasSize;
{$R *.lfm}
procedure TMainForm.AssignBitmap(b: TBitmap);
begin
with ScrollBox1 do
begin
HorzScrollBar.Range := b.Width;
VertScrollBar.Range := b.Height;
end;
Image1.SetBounds(0, 0, b.Width, b.Height);
Image1.Picture.Assign(b);
end;
procedure TMainForm.AssignPicture(b: TPicture);
begin
with ScrollBox1 do
begin
HorzScrollBar.Range := b.Width;
VertScrollBar.Range := b.Height;
end;
Image1.SetBounds(0, 0, b.Width, b.Height);
Image1.Picture.Assign(b);
end;
procedure TMainForm.CaptureArea;
var
ABitmap: TBitmap;
begin
with TAreaCapturer.Create(Application) do
try
if ShowModal = mrOk then
with fRect do
begin
if (Right > Left) and (Bottom > Top) then
begin
Sleep(DelayTime div 2);
ABitmap := TBitmap.Create;
ABitmap.Assign(CaptureScreenRect(fRect));
AssignBitmap(ABitmap);
ABitmap.Free;
end;
end;
finally
Free;
end;
end;
procedure TMainForm.OptionBGColorClick(Sender: TObject);
begin
if ColorDialog1.Execute then
BGColor.Brush.Color := ColorDialog1.Color;
end;
procedure TMainForm.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
CurrColor.Brush.Color := Image1.Picture.Bitmap.Canvas.Pixels[x, y];
end;
procedure TMainForm.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BGColor.Brush.Color := Image1.Picture.Bitmap.Canvas.Pixels[x, y];
end;
procedure TMainForm.ActionIsolateClick(Sender: TObject);
var
R, Q: TRect;
x, y: Integer;
Found: Boolean;
b: TBitmap;
begin
{ search for left side }
Found := False;
x := 0;
repeat
y := 0;
repeat
if Image1.Canvas.Pixels[x, y] <> BGColor.Brush.Color then
Found := True
else
y := y + 1;
until Found or (y >= Image1.Picture.Height);
if not Found then
x := x + 1;
until Found or (x >= Image1.Picture.Width);
if not Found then
MessageDlg('Η εικόνα είναι κενή...', mtError, [mbOK], 0)
else
begin
R.Left := x;
{ search for top side }
Found := False;
y := 0;
repeat
x := 0;
repeat
if Image1.Canvas.Pixels[x, y] <> BGColor.Brush.Color then
Found := True
else
x := x + 1;
until Found or (x >= Image1.Picture.Width);
if not Found then
y := y + 1;
until Found or (y >= Image1.Picture.Height);
R.Top := y;
{ search for right side }
Found := False;
x := Image1.Width - 1;
repeat
y := 0;
repeat
if Image1.Canvas.Pixels[x, y] <> BGColor.Brush.Color then
Found := True
else
y := y + 1;
until Found or (y >= Image1.Picture.Height);
if not Found then
x := x - 1;
until Found or (x < 0);
R.Right := x + 1;
{ search for bottom side }
Found := False;
y := Image1.Height - 1;
repeat
x := 0;
repeat
if Image1.Canvas.Pixels[x, y] <> BGColor.Brush.Color then
Found := True
else
x := x + 1;
until Found or (x >= Image1.Picture.Width);
if not Found then
y := y - 1;
until Found or (y < 0);
R.Bottom := y + 1;
Q := Rect(0, 0, R.Right - R.Left, R.Bottom - R.Top);
b := TBitmap.Create;
b.Width := Q.Right;
b.Height := Q.Bottom;
b.Canvas.CopyRect(Q, Image1.Picture.Bitmap.Canvas, R);
AssignBitmap(b);
{ ScrollBox2.VertScrollBar.Position :=0;
ScrollBox2.HorzScrollBar.Position :=0;
Image2.SetBounds(0, 0, Q.Right, Q.Bottom);
Image2.Picture.Assign(b);}
b.Free;
end;
end;
procedure TMainForm.ActionAlignClick(Sender: TObject);
var
w, h: Integer;
b: TBitmap;
R1: TRect;
R2: TRect;
begin
w := Image1.Width;
h := Image1.Height;
if GetNewCanvasSize(w, h) then
begin
b := TBitmap.Create;
b.Width := w;
b.Height := h;
b.Canvas.Brush.Color := BGColor.Brush.Color;
b.Canvas.FillRect(Bounds(0, 0, w, h));
R2 := Bounds(0, 0, Image1.Width, Image1.Height);
R1 := Rect((w - Image1.Width) div 2, (h - Image1.Height) div 2,
(w + Image1.Width) div 2, (h + Image1.Height) div 2);
b.Canvas.CopyRect(R1, Image1.Canvas, R2);
AssignBitmap(b);
b.Free;
end;
end;
procedure TMainForm.ActionTileClick(Sender: TObject);
var
w, h: Integer;
b: TBitmap;
R1: TRect;
R2: TRect;
begin
w := Image1.Width;
h := Image1.Height;
if GetNewCanvasSize(w, h) then
begin
b := TBitmap.Create;
b.Width := w;
b.Height := h;
R2 := Bounds(0, 0, Image1.Width, Image1.Height);
R1 := R2;
repeat
repeat
b.Canvas.CopyRect(R1, Image1.Canvas, R2);
R1.Left := R1.Left + Image1.Width;
R1.Right := R1.Left + Image1.Width;
until R1.Left > w;
R1 := Rect(0, R1.Top + Image1.Height, Image1.Width, R1.Bottom + Image1.Height);
until R1.Top > h;
AssignBitmap(b);
b.Free;
end;
end;
procedure TMainForm.EditUndoClick(Sender: TObject);
begin
end;
procedure TMainForm.EditRedoClick(Sender: TObject);
begin
end;
procedure TMainForm.EditCutClick(Sender: TObject);
begin
end;
procedure TMainForm.EditCopyClick(Sender: TObject);
begin
Clipboard.Assign(Image1.Picture);
end;
procedure TMainForm.EditCopyToFileClick(Sender: TObject);
begin
with SavePictureDialog1 do
if Execute then
Image1.Picture.SaveToFile(FileName);
end;
procedure TMainForm.EditPasteClick(Sender: TObject);
var
bmp1: TBitmap;
begin
if Clipboard.HasPictureFormat then
begin
bmp1 := TBitmap.Create;
try
bmp1.Assign(Clipboard);
AssignBitmap(bmp1);
finally
bmp1.Free;
end;
end
else
MessageDlg('Το πρόχειρο δεν έχει εικόνα', mtError, [mbOK], 0);
end;
procedure TMainForm.EditPasteFromFileClick(Sender: TObject);
var
p1: TPicture;
begin
with OpenPictureDialog1 do
if Execute then
begin
p1 := TPicture.Create;
try
p1.LoadFromFile(FileName);
AssignPicture(p1);
finally
p1.Free;
end;
end;
end;
procedure TMainForm.EditCaptureClick(Sender: TObject);
begin
Application.Minimize;
Sleep(DelayTime div 2);
CaptureArea;
Application.Restore;
end;
end.