-
Notifications
You must be signed in to change notification settings - Fork 10
/
ResimRamka.cs
250 lines (219 loc) · 5.61 KB
/
ResimRamka.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
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
/*
* Created by SharpDevelop.
* User: nk1449
* Date: 2014/11/04
* Time: 8:27
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace UyghurOCR
{
/// <summary>
/// Description of ResimRamka.
/// </summary>
///
public partial class ResimRamka : UserControl
{
public delegate void dlSeletedImage(Bitmap org);
public delegate void dlSelectedArea(int x, int y, int w, int h);
public delegate void dlMousePoint(int x, int y);
private Bitmap img_ORG=null;
private float _rt=1.0f;
private int _showX=0,_showY=0;
private int _showW=0,_showH=0;
private int _selX=-1,_selY=-1;
private int _fW=-1;
private int _fH=-1;
private bool _isPainting=false;
private bool _isBesildi=false;
private Pen talrayon=new Pen(Color.Red);
private Pen penOtturi=new Pen(Color.SkyBlue);
public dlSeletedImage Selected = null;
public dlSelectedArea SelectedArea = null;
public dlMousePoint MousePoint = null;
public dlMousePoint RightMouseClick = null;
private int _stBasX=0;
private int _stBasY=0;
private int _stSelX=0;
private int _stSelY=0;
private int _stSelW=0;
private int _stSelH=0;
public ResimRamka()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer|ControlStyles.ResizeRedraw, true);
this.UpdateStyles();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
public Bitmap Image{
set{
_selX=-1;
_selY=-1;
_fW = -1;
_fH = -1;
if(value!=null){
if(img_ORG!=null){
img_ORG.Dispose();
}
img_ORG = value;
}
Invalidate();
}
get{
return img_ORG;
}
}
void ResimRamkaPaint(object sender, PaintEventArgs e)
{
if(_isPainting)return;
_isPainting=true;
int x=0,y=0;
float rtw,rth;
int panW=this.Width;
int panH=this.Height;
int imgH,imgW;
if(img_ORG!=null){
imgH=img_ORG.Height;
imgW=img_ORG.Width;
rth=(float)panH/(float)imgH;
rtw=(float)panW/(float)imgW;
if(rth>rtw){
_rt=rtw;
}
else{
_rt=rth;
}
_showW=(int)(imgW*_rt);
_showH=(int)(imgH*_rt);
if (panW > _showW) {
x = (panW - _showW) / 2;
}
if (panH > _showH) {
y = (panH - _showH) / 2;
}
_showX=x;
_showY=y;
e.Graphics.InterpolationMode=InterpolationMode.HighQualityBilinear;
e.Graphics.DrawImage(img_ORG, _showX,_showY, _showW, _showH);
if((_fW!=-1) && (_fH!=-1))
{
e.Graphics.DrawRectangle(talrayon,(int)(_selX*_rt)+_showX,(int)(_selY*_rt)+_showY,(int)(_fW*_rt),(int)(_fH*_rt));
}
}
_isPainting=false;
}
void ResimRamkaMouseMove(object sender, MouseEventArgs e)
{
int w,h,posx=0,posy=0;
int difX,difY;
if(img_ORG==null) return;
if(e.Button== MouseButtons.Left && _isBesildi ){
w = (int) (img_ORG.Width * _rt);
h = (int) (img_ORG.Height * _rt);
difX=_stBasX-e.X;
difY=_stBasY-e.Y;
posy=(int)(_stSelH-difY);
posx=(int)(_stSelW-difX);
if(posx<0)posx=0;
if(posy<0)posy=0;
if((posy+_selY*_rt)<h){
_fH=(int)(posy/_rt);
}
if((posx+_selX*_rt)<w){
_fW=(int)(posx/_rt);
}
Invalidate();
}
else if(MousePoint!=null){
posx=(int)(( e.X-_showX)/_rt);
posy=(int)((e.Y-_showY)/_rt);
if(posx<0)
posx = 0;
if(posx >= img_ORG.Width)
posx = img_ORG.Width-1;
if(posy<0)
posy = 0;
if(posy >= img_ORG.Height)
posy = img_ORG.Height-1;
MousePoint(posx,posy);
}
}
void ResimRamkaMouseDown(object sender, MouseEventArgs e)
{
if(img_ORG==null) return;
if(e.Button== MouseButtons.Left){
_isBesildi=true;
_stBasX = e.X;
_stBasY = e.Y;
_selX=(int)((_stBasX-_showX)/_rt);
_selY=(int)((_stBasY-_showY)/_rt);
_fW= -1;
_fH = -1;
_stSelX=(int)(_selX*_rt);
_stSelY=(int)(_selY*_rt);
_stSelW=(int)(_fW*_rt);
_stSelH=(int)(_fH*_rt);
}
else{
_isBesildi = false;
}
Invalidate();
}
void ResimRamkaMouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left){
if(Selected!=null && _fH>0 && _fW>0){
Selected(img_ORG.Clone(new Rectangle(_selY,_selY+_fH,_selX,_selX+_fW),img_ORG.PixelFormat));
}
if(SelectedArea!=null && _fH>0 && _fW>0){
SelectedArea(_selX,_selY, _fW, _fH);
}
_isBesildi = false;
}
}
public void SetROI(int x, int y, int w, int h)
{
_selX = x;
_selY = y;
_fW = w;
_fH = h;
Invalidate();
}
public Rectangle getRoi(){
if(_fW!=-1 && _fH!=-1)
{
return new Rectangle(_selX,_selY,_fW,_fH);
}
else{
return new Rectangle(0,0,img_ORG.Width,img_ORG.Height);
}
}
void ResimRamkaMouseClick(object sender, MouseEventArgs e)
{
int posx=0,posy=0;
if(img_ORG==null) return;
if(e.Button == MouseButtons.Right && RightMouseClick!=null){
posx=(int)(( e.X-_showX)/_rt);
posy=(int)((e.Y-_showY)/_rt);
if(posx<0)
posx = 0;
if(posx >= img_ORG.Width)
posx = img_ORG.Width-1;
if(posy<0)
posy = 0;
if(posy >= img_ORG.Height)
posy = img_ORG.Height-1;
RightMouseClick(posx,posy);
}
}
}
}