CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | using System.Drawing; |
||
4 | using System.Drawing.Drawing2D; |
||
5 | using System.Globalization; |
||
6 | using System.Runtime.Serialization; |
||
7 | using System.Windows.Forms; |
||
8 | |||
9 | namespace CraftSynth.ImageEditor |
||
10 | { |
||
11 | /// <summary> |
||
12 | /// Image graphic object |
||
13 | /// </summary> |
||
14 | //[Serializable] |
||
15 | public class DrawImage : DrawObject |
||
16 | { |
||
17 | public Rectangle rectangle; |
||
18 | private Bitmap _image; |
||
19 | // this holds the original image unscaled |
||
20 | private Bitmap _originalImage; |
||
21 | public bool IsInitialImage; |
||
22 | |||
23 | public Bitmap TheImage |
||
24 | { |
||
25 | get { return _image; } |
||
26 | set |
||
27 | { |
||
28 | _originalImage = value; |
||
29 | ResizeImage(rectangle.Width, rectangle.Height); |
||
30 | } |
||
31 | } |
||
32 | |||
33 | private const string entryRectangle = "Rect"; |
||
34 | private const string entryImage = "Image"; |
||
35 | private const string entryImageOriginal = "OriginalImage"; |
||
36 | |||
37 | private bool _disposed = false; |
||
38 | |||
39 | /// <summary> |
||
40 | /// Clone this instance |
||
41 | /// </summary> |
||
42 | public override DrawObject Clone() |
||
43 | { |
||
44 | DrawImage drawImage = new DrawImage(); |
||
45 | drawImage._image = _image; |
||
46 | drawImage._originalImage = _originalImage; |
||
47 | drawImage.rectangle = rectangle; |
||
48 | drawImage.IsInitialImage = IsInitialImage; |
||
49 | |||
50 | FillDrawObjectFields(drawImage); |
||
51 | return drawImage; |
||
52 | } |
||
53 | |||
54 | protected Rectangle Rectangle |
||
55 | { |
||
56 | get { return rectangle; } |
||
57 | set { rectangle = value; } |
||
58 | } |
||
59 | |||
60 | #region Destruction |
||
61 | protected override void Dispose(bool disposing) |
||
62 | { |
||
63 | if (!this._disposed) |
||
64 | { |
||
65 | if (disposing) |
||
66 | { |
||
67 | // Free any managed objects here. |
||
68 | if (this._originalImage!=null) |
||
69 | { |
||
70 | this._originalImage.Dispose(); |
||
71 | } |
||
72 | if (this._image!=null) |
||
73 | { |
||
74 | this._image.Dispose(); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // Free any unmanaged objects here. |
||
79 | |||
80 | this._disposed = true; |
||
81 | } |
||
82 | base.Dispose(disposing); |
||
83 | } |
||
84 | |||
85 | ~DrawImage() |
||
86 | { |
||
87 | this.Dispose(false); |
||
88 | } |
||
89 | #endregion |
||
90 | |||
91 | public DrawImage() |
||
92 | { |
||
93 | SetRectangle(0, 0, 1, 1); |
||
94 | Initialize(); |
||
95 | } |
||
96 | |||
97 | public DrawImage(int x, int y, bool isInitialImage) |
||
98 | { |
||
99 | rectangle.X = x; |
||
100 | rectangle.Y = y; |
||
101 | rectangle.Width = 1; |
||
102 | rectangle.Height = 1; |
||
103 | this.IsInitialImage = isInitialImage; |
||
104 | Initialize(); |
||
105 | } |
||
106 | |||
107 | public DrawImage(int x, int y, Bitmap image) |
||
108 | { |
||
109 | rectangle.X = x; |
||
110 | rectangle.Y = y; |
||
111 | _image = (Bitmap)image.Clone(); |
||
112 | SetRectangle(rectangle.X, rectangle.Y, image.Width, image.Height); |
||
113 | Center = new Point(x + (image.Width / 2), y + (image.Height / 2)); |
||
114 | TipText = String.Format("Image Center @ {0}, {1}", Center.X, Center.Y); |
||
115 | Initialize(); |
||
116 | } |
||
117 | |||
118 | /// <summary> |
||
119 | /// Draw image |
||
120 | /// </summary> |
||
121 | /// <param name="g"></param> |
||
122 | public override void Draw(Graphics g) |
||
123 | { |
||
124 | // Get existing World transformation |
||
125 | Matrix mSave = g.Transform; |
||
126 | if (Rotation != 0) |
||
127 | { |
||
128 | Matrix m = mSave.Clone(); |
||
129 | m.RotateAt(Rotation, new PointF(rectangle.Left + (rectangle.Width / 2), rectangle.Top + (rectangle.Height / 2)), MatrixOrder.Append); |
||
130 | g.Transform = m; |
||
131 | } |
||
132 | if (_image == null) |
||
133 | { |
||
134 | Pen p = new Pen(Color.Black, -1f); |
||
135 | g.DrawRectangle(p, rectangle); |
||
136 | } else |
||
137 | g.DrawImage(_image, new Point(rectangle.X, rectangle.Y)); |
||
138 | // Restore World transformation |
||
139 | g.Transform = mSave; |
||
140 | } |
||
141 | |||
142 | protected void SetRectangle(int x, int y, int width, int height) |
||
143 | { |
||
144 | rectangle.X = x; |
||
145 | rectangle.Y = y; |
||
146 | rectangle.Width = width; |
||
147 | rectangle.Height = height; |
||
148 | } |
||
149 | |||
150 | /// <summary> |
||
151 | /// Get number of handles |
||
152 | /// </summary> |
||
153 | public override int HandleCount |
||
154 | { |
||
155 | get { return 8; } |
||
156 | } |
||
157 | |||
158 | /// <summary> |
||
159 | /// Get handle point by 1-based number |
||
160 | /// </summary> |
||
161 | /// <param name="handleNumber"></param> |
||
162 | /// <returns></returns> |
||
163 | public override Point GetHandle(int handleNumber) |
||
164 | { |
||
165 | int x, y, xCenter, yCenter; |
||
166 | |||
167 | xCenter = rectangle.X + rectangle.Width / 2; |
||
168 | yCenter = rectangle.Y + rectangle.Height / 2; |
||
169 | x = rectangle.X; |
||
170 | y = rectangle.Y; |
||
171 | |||
172 | switch (handleNumber) |
||
173 | { |
||
174 | case 1: |
||
175 | x = rectangle.X; |
||
176 | y = rectangle.Y; |
||
177 | break; |
||
178 | case 2: |
||
179 | x = xCenter; |
||
180 | y = rectangle.Y; |
||
181 | break; |
||
182 | case 3: |
||
183 | x = rectangle.Right; |
||
184 | y = rectangle.Y; |
||
185 | break; |
||
186 | case 4: |
||
187 | x = rectangle.Right; |
||
188 | y = yCenter; |
||
189 | break; |
||
190 | case 5: |
||
191 | x = rectangle.Right; |
||
192 | y = rectangle.Bottom; |
||
193 | break; |
||
194 | case 6: |
||
195 | x = xCenter; |
||
196 | y = rectangle.Bottom; |
||
197 | break; |
||
198 | case 7: |
||
199 | x = rectangle.X; |
||
200 | y = rectangle.Bottom; |
||
201 | break; |
||
202 | case 8: |
||
203 | x = rectangle.X; |
||
204 | y = yCenter; |
||
205 | break; |
||
206 | } |
||
207 | return new Point(x, y); |
||
208 | } |
||
209 | |||
210 | /// <summary> |
||
211 | /// Hit test. |
||
212 | /// Return value: -1 - no hit |
||
213 | /// 0 - hit anywhere |
||
214 | /// > 1 - handle number |
||
215 | /// </summary> |
||
216 | /// <param name="point"></param> |
||
217 | /// <returns></returns> |
||
218 | public override int HitTest(Point point) |
||
219 | { |
||
220 | if (Selected) |
||
221 | { |
||
222 | for (int i = 1; i <= HandleCount; i++) |
||
223 | { |
||
224 | if (GetHandleRectangle(i).Contains(point)) |
||
225 | return i; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | if (PointInObject(point)) |
||
230 | return 0; |
||
231 | return -1; |
||
232 | } |
||
233 | |||
234 | protected override bool PointInObject(Point point) |
||
235 | { |
||
236 | return rectangle.Contains(point); |
||
237 | } |
||
238 | |||
239 | public override Rectangle GetBounds(Graphics g) |
||
240 | { |
||
241 | return rectangle; |
||
242 | } |
||
243 | |||
244 | /// <summary> |
||
245 | /// Get cursor for the handle |
||
246 | /// </summary> |
||
247 | /// <param name="handleNumber"></param> |
||
248 | /// <returns></returns> |
||
249 | public override Cursor GetHandleCursor(int handleNumber) |
||
250 | { |
||
251 | switch (handleNumber) |
||
252 | { |
||
253 | case 1: |
||
254 | return Cursors.SizeNWSE; |
||
255 | case 2: |
||
256 | return Cursors.SizeNS; |
||
257 | case 3: |
||
258 | return Cursors.SizeNESW; |
||
259 | case 4: |
||
260 | return Cursors.SizeWE; |
||
261 | case 5: |
||
262 | return Cursors.SizeNWSE; |
||
263 | case 6: |
||
264 | return Cursors.SizeNS; |
||
265 | case 7: |
||
266 | return Cursors.SizeNESW; |
||
267 | case 8: |
||
268 | return Cursors.SizeWE; |
||
269 | default: |
||
270 | return Cursors.Default; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | /// <summary> |
||
275 | /// Move handle to new point (resizing) |
||
276 | /// </summary> |
||
277 | /// <param name="point"></param> |
||
278 | /// <param name="handleNumber"></param> |
||
279 | public override void MoveHandleTo(Point point, int handleNumber) |
||
280 | { |
||
281 | int left = Rectangle.Left; |
||
282 | int top = Rectangle.Top; |
||
283 | int right = Rectangle.Right; |
||
284 | int bottom = Rectangle.Bottom; |
||
285 | |||
286 | switch (handleNumber) |
||
287 | { |
||
288 | case 1: |
||
289 | left = point.X; |
||
290 | top = point.Y; |
||
291 | break; |
||
292 | case 2: |
||
293 | top = point.Y; |
||
294 | break; |
||
295 | case 3: |
||
296 | right = point.X; |
||
297 | top = point.Y; |
||
298 | break; |
||
299 | case 4: |
||
300 | right = point.X; |
||
301 | break; |
||
302 | case 5: |
||
303 | right = point.X; |
||
304 | bottom = point.Y; |
||
305 | break; |
||
306 | case 6: |
||
307 | bottom = point.Y; |
||
308 | break; |
||
309 | case 7: |
||
310 | left = point.X; |
||
311 | bottom = point.Y; |
||
312 | break; |
||
313 | case 8: |
||
314 | left = point.X; |
||
315 | break; |
||
316 | } |
||
317 | Dirty = true; |
||
318 | SetRectangle(left, top, right - left, bottom - top); |
||
319 | ResizeImage(rectangle.Width, rectangle.Height); |
||
320 | } |
||
321 | |||
322 | protected void ResizeImage(int width, int height) |
||
323 | { |
||
324 | if (_originalImage != null) |
||
325 | { |
||
326 | Bitmap b = new Bitmap(_originalImage, new Size(width, height)); |
||
327 | _image = (Bitmap)b.Clone(); |
||
328 | b.Dispose(); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | public override bool IntersectsWith(Rectangle rectangle) |
||
333 | { |
||
334 | return Rectangle.IntersectsWith(rectangle); |
||
335 | } |
||
336 | |||
337 | /// <summary> |
||
338 | /// Move object |
||
339 | /// </summary> |
||
340 | /// <param name="deltaX"></param> |
||
341 | /// <param name="deltaY"></param> |
||
342 | public override void Move(int deltaX, int deltaY) |
||
343 | { |
||
344 | rectangle.X += deltaX; |
||
345 | rectangle.Y += deltaY; |
||
346 | Dirty = true; |
||
347 | } |
||
348 | |||
349 | public override void Dump() |
||
350 | { |
||
351 | base.Dump(); |
||
352 | |||
353 | Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture)); |
||
354 | Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture)); |
||
355 | Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture)); |
||
356 | Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture)); |
||
357 | } |
||
358 | |||
359 | /// <summary> |
||
360 | /// Normalize rectangle |
||
361 | /// </summary> |
||
362 | public override void Normalize() |
||
363 | { |
||
364 | rectangle = DrawRectangle.GetNormalizedRectangle(rectangle); |
||
365 | } |
||
366 | |||
367 | /// <summary> |
||
368 | /// Save object to serialization stream |
||
369 | /// </summary> |
||
370 | /// <param name="info"></param> |
||
371 | /// <param name="orderNumber"></param> |
||
372 | /// <param name="objectIndex"></param> |
||
373 | public override void SaveToStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
374 | { |
||
375 | info.AddValue( |
||
376 | String.Format(CultureInfo.InvariantCulture, |
||
377 | "{0}{1}-{2}", |
||
378 | entryRectangle, orderNumber, objectIndex), |
||
379 | rectangle); |
||
380 | info.AddValue( |
||
381 | String.Format(CultureInfo.InvariantCulture, |
||
382 | "{0}{1}-{2}", |
||
383 | entryImage, orderNumber, objectIndex), |
||
384 | _image); |
||
385 | info.AddValue( |
||
386 | String.Format(CultureInfo.InvariantCulture, |
||
387 | "{0}{1}-{2}", |
||
388 | entryImageOriginal, orderNumber, objectIndex), |
||
389 | _originalImage); |
||
390 | |||
391 | base.SaveToStream(info, orderNumber, objectIndex); |
||
392 | } |
||
393 | |||
394 | /// <summary> |
||
395 | /// Load object from serialization stream |
||
396 | /// </summary> |
||
397 | /// <param name="info"></param> |
||
398 | /// <param name="orderNumber"></param> |
||
399 | /// <param name="objectIndex"></param> |
||
400 | public override void LoadFromStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
401 | { |
||
402 | rectangle = (Rectangle)info.GetValue( |
||
403 | String.Format(CultureInfo.InvariantCulture, |
||
404 | "{0}{1}-{2}", |
||
405 | entryRectangle, orderNumber, objectIndex), |
||
406 | typeof(Rectangle)); |
||
407 | _image = (Bitmap)info.GetValue( |
||
408 | String.Format(CultureInfo.InvariantCulture, |
||
409 | "{0}{1}-{2}", |
||
410 | entryImage, orderNumber, objectIndex), |
||
411 | typeof(Bitmap)); |
||
412 | _originalImage = (Bitmap)info.GetValue( |
||
413 | String.Format(CultureInfo.InvariantCulture, |
||
414 | "{0}{1}-{2}", |
||
415 | entryImageOriginal, orderNumber, objectIndex), |
||
416 | typeof(Bitmap)); |
||
417 | |||
418 | base.LoadFromStream(info, orderNumber, objectIndex); |
||
419 | } |
||
420 | |||
421 | #region Helper Functions |
||
422 | public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2) |
||
423 | { |
||
424 | if (x2 < x1) |
||
425 | { |
||
426 | int tmp = x2; |
||
427 | x2 = x1; |
||
428 | x1 = tmp; |
||
429 | } |
||
430 | |||
431 | if (y2 < y1) |
||
432 | { |
||
433 | int tmp = y2; |
||
434 | y2 = y1; |
||
435 | y1 = tmp; |
||
436 | } |
||
437 | return new Rectangle(x1, y1, x2 - x1, y2 - y1); |
||
438 | } |
||
439 | |||
440 | public static Rectangle GetNormalizedRectangle(Point p1, Point p2) |
||
441 | { |
||
442 | return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y); |
||
443 | } |
||
444 | |||
445 | public static Rectangle GetNormalizedRectangle(Rectangle r) |
||
446 | { |
||
447 | return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height); |
||
448 | } |
||
449 | #endregion Helper Functions |
||
450 | } |
||
451 | } |