1: using System;
2: using System.Drawing;
3: using System.Drawing.Imaging;
4: using System.Drawing.Drawing2D;
5: using System.IO;
6: using System.Security.Cryptography;
7:
8: #if WFA
9: using System.ComponentModel;
10: using System.Windows.Forms;
11: #else
12: using System.Web;
13: #endif
14:
15: namespace Ia.Cl.Model
16: {
17: ////////////////////////////////////////////////////////////////////////////
18:
19: /// <summary publish="true">
20: /// Image processing support class.
21: /// </summary>
22: /// <remarks>
23: /// Copyright � 2001-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
24: ///
25: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
26: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
27: ///
28: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
29: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
30: ///
31: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
32: ///
33: /// Copyright notice: This notice may not be removed or altered from any source distribution.
34: /// </remarks>
35: public class Image
36: {
37: ////////////////////////////////////////////////////////////////////////////
38:
39: /// <summary>
40: ///
41: /// </summary>
42: public Image()
43: {
44: }
45:
46: ////////////////////////////////////////////////////////////////////////////
47:
48: /// <summary>
49: ///
50: /// </summary>
51: public static void CreateGeneralUse(string pathSourceFile)
52: {
53: CreateGeneralUse(pathSourceFile, pathSourceFile, -1, -1);
54: }
55:
56: ////////////////////////////////////////////////////////////////////////////
57:
58: /// <summary>
59: ///
60: /// </summary>
61: public static void CreateGeneralUse(string pathSourceFile, string pathDestinationPath)
62: {
63: CreateGeneralUse(pathSourceFile, pathDestinationPath, -1, -1);
64: }
65:
66: ////////////////////////////////////////////////////////////////////////////
67:
68: /// <summary>
69: ///
70: /// </summary>
71: public static void CreateGeneralUse(string pathSourceFile, string pathDestinationPath, int num, int suffix)
72: {
73: // resize main image and generate images of the different formats
74: string name, extension;//, absolutePath;
75:
76: // we will check if paths are absolute or relative and adjust them accordingly.
77:
78: pathSourceFile = AbsolutePath(pathSourceFile);
79: pathDestinationPath = AbsolutePath(pathDestinationPath);
80:
81: try
82: {
83: System.Drawing.Image image = System.Drawing.Image.FromFile(pathSourceFile);
84:
85: extension = System.IO.Path.GetExtension(pathSourceFile);
86: extension = extension.ToLower();
87:
88: // this solves a bug:
89: image.RotateFlip(RotateFlipType.Rotate180FlipNone);
90: image.RotateFlip(RotateFlipType.Rotate180FlipNone);
91:
92: // below; this generates images that assume the original image has dimentions ration of 4:3 (1024:768)
93: // generate tiny, small, middle, and large size images and the sample image:
94:
95: // this will generate images that have a fixed "area". This will preserve the original width-height ratio, but will
96: // also give a scaled, suitable versions:
97:
98: // note that if w0*h0=A0 and w*h=Af, then h=h0*(sqrt(Af))/(sqrt(A)) and w=w0*(sqrt(Af))/(sqrt(A))
99:
100: double w, h, A, Af, r, w2, h2;
101: w = image.Width; h = image.Height;
102: A = w * h;
103:
104: System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
105:
106: Af = 640 * 480;
107: if (Af > A) Af = A;
108: r = Math.Sqrt(Af) / Math.Sqrt(A);
109: System.Drawing.Image very_large = image.GetThumbnailImage(Convert.ToInt32(w * r), Convert.ToInt32(h * r), dummyCallBack, IntPtr.Zero);
110:
111: Af = 400 * 300;
112: if (Af > A) Af = A;
113: r = Math.Sqrt(Af) / Math.Sqrt(A);
114: System.Drawing.Image large = image.GetThumbnailImage(Convert.ToInt32(w * r), Convert.ToInt32(h * r), dummyCallBack, IntPtr.Zero);
115:
116: Af = 200 * 150;
117: if (Af > A) Af = A;
118: r = Math.Sqrt(Af) / Math.Sqrt(A);
119: System.Drawing.Image middle = image.GetThumbnailImage(Convert.ToInt32(w * r), Convert.ToInt32(h * r), dummyCallBack, IntPtr.Zero);
120:
121: Af = 120 * 90;
122: if (Af > A) Af = A;
123: r = Math.Sqrt(Af) / Math.Sqrt(A);
124: System.Drawing.Image small = image.GetThumbnailImage(Convert.ToInt32(w * r), Convert.ToInt32(h * r), dummyCallBack, IntPtr.Zero);
125:
126: Af = 80 * 60;
127: if (Af > A) Af = A;
128: r = Math.Sqrt(Af) / Math.Sqrt(A);
129: System.Drawing.Image tiny = image.GetThumbnailImage(Convert.ToInt32(w * r), Convert.ToInt32(h * r), dummyCallBack, IntPtr.Zero);
130:
131: // this saves an exact size version:
132: System.Drawing.Image same = image.GetThumbnailImage(Convert.ToInt32(w), Convert.ToInt32(h), dummyCallBack, IntPtr.Zero);
133:
134: // the methods below will generate images with fixed width and varying high. Width range from 75 to 500.
135: System.Drawing.Image image_500, image_400, image_250, image_100, image_75;
136:
137: w2 = 500;
138: h2 = w2 * h / w;
139: image_500 = image.GetThumbnailImage(Convert.ToInt32(w2), Convert.ToInt32(h2), dummyCallBack, IntPtr.Zero);
140:
141: w2 = 400;
142: h2 = w2 * h / w;
143: image_400 = image.GetThumbnailImage(Convert.ToInt32(w2), Convert.ToInt32(h2), dummyCallBack, IntPtr.Zero);
144:
145: w2 = 250;
146: h2 = w2 * h / w;
147: image_250 = image.GetThumbnailImage(Convert.ToInt32(w2), Convert.ToInt32(h2), dummyCallBack, IntPtr.Zero);
148:
149: w2 = 100;
150: h2 = w2 * h / w;
151: image_100 = image.GetThumbnailImage(Convert.ToInt32(w2), Convert.ToInt32(h2), dummyCallBack, IntPtr.Zero);
152:
153: w2 = 75;
154: h2 = w2 * h / w;
155: image_75 = image.GetThumbnailImage(Convert.ToInt32(w2), Convert.ToInt32(h2), dummyCallBack, IntPtr.Zero);
156:
157: if (num < 0 && suffix < 0)
158: {
159: name = pathDestinationPath.Replace(".jpg", "");
160: }
161: else if (num < 0) name = pathDestinationPath + "_" + suffix;
162: else if (suffix < 0) name = pathDestinationPath + "_" + num;
163: else name = pathDestinationPath + num + "_" + suffix;
164:
165: //
166:
167: //middle = ImageShadow.OnPaint(middle);
168:
169: // save new images:
170:
171: if (extension == ".jpg")
172: {
173: SaveJpeg(name + "_vl.jpg", very_large, 100);
174: SaveJpeg(name + "_l.jpg", large, 100);
175: SaveJpeg(name + "_m.jpg", middle, 100);
176: SaveJpeg(name + "_s.jpg", small, 100);
177: SaveJpeg(name + "_t.jpg", tiny, 100);
178: SaveJpeg(name + ".jpg", same, 100);
179:
180: SaveJpeg(name + "_500.jpg", image_500, 100);
181: SaveJpeg(name + "_400.jpg", image_400, 100);
182: SaveJpeg(name + "_250.jpg", image_250, 100);
183: SaveJpeg(name + "_100.jpg", image_100, 100);
184: SaveJpeg(name + "_75.jpg", image_75, 100);
185: }
186: else
187: {
188: very_large.Save(name + "_vl.png", ImageFormat.Png);
189: very_large.Dispose();
190:
191: large.Save(name + "_l.png", ImageFormat.Png);
192: large.Dispose();
193:
194: middle.Save(name + "_m.png", ImageFormat.Png);
195: middle.Dispose();
196:
197: small.Save(name + "_s.png", ImageFormat.Png);
198: small.Dispose();
199:
200: tiny.Save(name + "_t.png", ImageFormat.Png);
201: tiny.Dispose();
202:
203: same.Save(name + ".png", ImageFormat.Png);
204: same.Dispose();
205:
206:
207: image_500.Save(name + "_500.png", ImageFormat.Png);
208: image_500.Dispose();
209:
210: image_400.Save(name + "_400.png", ImageFormat.Png);
211: image_400.Dispose();
212:
213: image_250.Save(name + "_250.png", ImageFormat.Png);
214: image_250.Dispose();
215:
216: image_100.Save(name + "_100.png", ImageFormat.Png);
217: image_100.Dispose();
218:
219: image_75.Save(name + "_75.png", ImageFormat.Png);
220: image_75.Dispose();
221: }
222:
223: // cut a middle section of the main image to produce a suitable sample image:
224: /*
225:
226: System.Drawing.Image sample = image.GetThumbnailImage(100, 300, dummyCallBack, IntPtr.Zero);
227:
228: Graphics graphic = Graphics.FromImage(sample);
229:
230: try
231: {
232: RectangleF dest = new RectangleF(0, 0, 100, 400);
233: RectangleF source = new RectangleF(150, 0, 100, 400);
234: graphic.DrawImage(large, dest, source, GraphicsUnit.Pixel);
235:
236: if (extension == ".jpg") sample.Save(name + "_sample.jpg", ImageFormat.Jpeg);
237: else sample.Save(name + "_sample.png", ImageFormat.Png);
238:
239: sample.Dispose();
240: }
241: catch (Exception)
242: {
243: //result_l.Text += " "+ex.ToString();
244: }
245: finally
246: {
247: if (null != graphic) graphic.Dispose();
248: }
249: */
250:
251: image.Dispose();
252: }
253: catch (Exception)
254: {
255: }
256: }
257:
258: /*
259: ////////////////////////////////////////////////////////////////////////////
260:
261: /// <summary>
262: ///
263: /// </summary>
264:
265: public class ImageShadow : Image
266: {
267: private Color _panelColor;
268:
269: public Color PanelColor
270: {
271: get { return _panelColor; }
272: set { _panelColor = value; }
273: }
274:
275: private Color _borderColor;
276:
277: public Color BorderColor
278: {
279: get { return _borderColor; }
280: set { _borderColor = value; }
281: }
282:
283: private int shadowSize = 5;
284: private int shadowMargin = 2;
285:
286: // static for good perfomance
287: static Image shadowDownRight = new Bitmap(typeof(ImageShadow), "Images.tshadowdownright.png");
288: static Image shadowDownLeft = new Bitmap(typeof(ImageShadow), "Images.tshadowdownleft.png");
289: static Image shadowDown = new Bitmap(typeof(ImageShadow), "Images.tshadowdown.png");
290: static Image shadowRight = new Bitmap(typeof(ImageShadow), "Images.tshadowright.png");
291: static Image shadowTopRight = new Bitmap(typeof(ImageShadow), "Images.tshadowtopright.png");
292:
293: public ImageShadow()
294: {
295: }
296:
297: public static System.Drawing.Image OnPaint(System.Drawing.Image i)
298: {
299: // Get the graphics object. We need something to draw with ;-)
300: Graphics g = Graphics.FromImage(i);
301:
302: // Create tiled brushes for the shadow on the right and at the bottom.
303: TextureBrush shadowRightBrush = new TextureBrush(shadowRight, WrapMode.Tile);
304: TextureBrush shadowDownBrush = new TextureBrush(shadowDown, WrapMode.Tile);
305:
306: // Translate (move) the brushes so the top or left of the image matches the top or left of the
307: // area where it's drawed. If you don't understand why this is necessary, comment it out.
308: // Hint: The tiling would start at 0,0 of the control, so the shadows will be offset a little.
309: shadowDownBrush.TranslateTransform(0, Height - shadowSize);
310: shadowRightBrush.TranslateTransform(Width - shadowSize, 0);
311:
312: // Define the rectangles that will be filled with the brush.
313: // (where the shadow is drawn)
314: Rectangle shadowDownRectangle = new Rectangle(
315: shadowSize + shadowMargin, // X
316: Height - shadowSize, // Y
317: Width - (shadowSize * 2 + shadowMargin), // width (stretches)
318: shadowSize // height
319: );
320:
321: Rectangle shadowRightRectangle = new Rectangle(
322: Width - shadowSize, // X
323: shadowSize + shadowMargin, // Y
324: shadowSize, // width
325: Height - (shadowSize * 2 + shadowMargin) // height (stretches)
326: );
327:
328: // And draw the shadow on the right and at the bottom.
329: g.FillRectangle(shadowDownBrush, shadowDownRectangle);
330: g.FillRectangle(shadowRightBrush, shadowRightRectangle);
331:
332: // Now for the corners, draw the 3 5x5 pixel images.
333: g.DrawImage(shadowTopRight, new Rectangle(Width - shadowSize, shadowMargin, shadowSize, shadowSize));
334: g.DrawImage(shadowDownRight, new Rectangle(Width - shadowSize, Height - shadowSize, shadowSize, shadowSize));
335: g.DrawImage(shadowDownLeft, new Rectangle(shadowMargin, Height - shadowSize, shadowSize, shadowSize));
336:
337: // Fill the area inside with the color in the PanelColor property.
338: // 1 pixel is added to everything to make the rectangle smaller.
339: // This is because the 1 pixel border is actually drawn outside the rectangle.
340: Rectangle fullRectangle = new Rectangle(
341: 1, // X
342: 1, // Y
343: Width - (shadowSize + 2), // Width
344: Height - (shadowSize + 2) // Height
345: );
346:
347: if (PanelColor != null)
348: {
349: SolidBrush bgBrush = new SolidBrush(_panelColor);
350: g.FillRectangle(bgBrush, fullRectangle);
351: }
352:
353: // Draw a nice 1 pixel border it a BorderColor is specified
354: if (_borderColor != null)
355: {
356: Pen borderPen = new Pen(BorderColor);
357: g.DrawRectangle(borderPen, fullRectangle);
358: }
359:
360: // Memory efficiency
361: shadowDownBrush.Dispose();
362: shadowRightBrush.Dispose();
363:
364: shadowDownBrush = null;
365: shadowRightBrush = null;
366: }
367: }
368:
369: */
370:
371: ////////////////////////////////////////////////////////////////////////////
372:
373: /// <summary>
374: ///
375: /// </summary>
376: public static bool SaveJpeg(string sFileName, System.Drawing.Image img, long nQuality)
377: {
378: //Syntax: SaveJpeg(filename, image object, quality (1-100))
379:
380: try
381: {
382: // Build image encoder detail
383: ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
384: EncoderParameters encoderParameters = new EncoderParameters(1);
385: encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, nQuality);
386: // Data used "System.Drawing.Imaging" because there is another Encoder in System.Text
387:
388: // Get jpg format id
389: foreach (ImageCodecInfo encoder in encoders)
390: {
391: if (encoder.MimeType == "image/jpeg")
392: {
393: // Save
394: img.Save(sFileName, encoder, encoderParameters);
395: img.Dispose();
396: return true;
397: }
398: }
399:
400: //result_l.Text += " Suitable JPEG encoder format not found";
401: }
402: catch (Exception)
403: {
404: //result_l.Text += " Error saving:"+sFileName+":"+ex.Message;
405: }
406: return false;
407: }
408:
409: ////////////////////////////////////////////////////////////////////////////
410:
411: /// <summary>
412: /// Delete all images generated from CreateGeneralUseImages() function
413: /// <param name="relative_path">Folder path of all images to be deleted</param>
414: /// <param name="num">Designated main number of all images</param>
415: /// <param name="suffix">Suffix image number</param>
416: /// </summary>
417: public static void DeleteGeneralUse(string relative_path, int num, int suffix)
418: {
419: int count;
420: string name, absolutePath, r;
421: string[] p;
422:
423: absolutePath = AbsolutePath();
424:
425: if (num < 0 && suffix < 0) name = absolutePath + relative_path;
426: else if (num < 0) name = absolutePath + relative_path + "_" + suffix;
427: else if (suffix < 0) name = absolutePath + relative_path + "_" + num;
428: else name = absolutePath + relative_path + num + "_" + suffix;
429:
430: File.Delete(name + "_vl.jpg");
431: File.Delete(name + "_l.jpg");
432: File.Delete(name + "_m.jpg");
433: File.Delete(name + "_s.jpg");
434: File.Delete(name + "_t.jpg");
435: File.Delete(name + ".jpg");
436:
437: File.Delete(name + "_500.jpg");
438: File.Delete(name + "_400.jpg");
439: File.Delete(name + "_250.jpg");
440: File.Delete(name + "_100.jpg");
441: File.Delete(name + "_75.jpg");
442:
443: // rearrange images so they stay in sequence
444: try
445: {
446: // rename all following images so that they fill the name of the missing image:
447: // count images:
448: p = Directory.GetFiles(absolutePath + relative_path, num + @"_*_t.jpg");
449:
450: count = p.Length;
451:
452: for (int i = suffix; i < count; i++)
453: {
454: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_vl.jpg", absolutePath + relative_path + num + "_" + i + "_vl.jpg");
455: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_l.jpg", absolutePath + relative_path + num + "_" + i + "_l.jpg");
456: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_m.jpg", absolutePath + relative_path + num + "_" + i + "_m.jpg");
457: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_s.jpg", absolutePath + relative_path + num + "_" + i + "_s.jpg");
458: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_t.jpg", absolutePath + relative_path + num + "_" + i + "_t.jpg");
459: //File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_sample.jpg", absolutePath + relative_path + num + "_" + i + "_sample.jpg");
460: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + ".jpg", absolutePath + relative_path + num + "_" + i + ".jpg");
461:
462: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_500.jpg", absolutePath + relative_path + num + "_" + i + "_500.jpg");
463: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_400.jpg", absolutePath + relative_path + num + "_" + i + "_400.jpg");
464: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_250.jpg", absolutePath + relative_path + num + "_" + i + "_250.jpg");
465: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_100.jpg", absolutePath + relative_path + num + "_" + i + "_100.jpg");
466: File.Move(absolutePath + relative_path + num + "_" + (i + 1) + "_75.jpg", absolutePath + relative_path + num + "_" + i + "_75.jpg");
467: }
468: }
469: catch (Exception ex)
470: {
471: #if DEBUG
472: r = "Error: " + ex.ToString();
473: #else
474: r = "Error: " + ex.Message;
475: #endif
476: }
477: finally
478: {
479: }
480: }
481:
482: ////////////////////////////////////////////////////////////////////////////
483:
484: /// <summary>
485: /// Delete all images generated from CreateGeneralUseImages() function
486: /// <param name="relative_path">Folder path of all images to be deleted</param>
487: /// <param name="num">Designated main number of all images</param>
488: /// </summary>
489: public static void DeleteGeneralUse(string relative_path, int num)
490: {
491: string absolutePath;
492:
493: absolutePath = AbsolutePath();
494:
495: foreach (string file in Directory.GetFiles(absolutePath + relative_path, num + "_*.jpg"))
496: {
497: System.IO.File.Delete(file);
498: }
499: }
500:
501: ////////////////////////////////////////////////////////////////////////////
502:
503: /// <summary>
504: /// Move images from a suffix to another
505: /// <param name="relative_path">Folder path of all images to be moved</param>
506: /// <param name="num">Designated main number of all images</param>
507: /// <param name="suffix_old">Suffix image number</param>
508: /// <param name="suffix_new">New suffix image number</param>
509: /// </summary>
510: public static void MoveGeneralUse(string relative_path, int num, int suffix_old, int suffix_new)
511: {
512: string name, name_new, absolutePath;
513:
514: absolutePath = AbsolutePath();
515:
516: if (num < 0 && suffix_old < 0)
517: {
518: name = name_new = absolutePath + relative_path;
519: }
520: else if (num < 0)
521: {
522: name = absolutePath + relative_path + "_" + suffix_old;
523: name_new = absolutePath + relative_path + "_" + suffix_new;
524: }
525: else if (suffix_old < 0)
526: {
527: name = name_new = absolutePath + relative_path + "_" + num;
528: }
529: else
530: {
531: name = absolutePath + relative_path + num + "_" + suffix_old;
532: name_new = absolutePath + relative_path + num + "_" + suffix_new;
533: }
534:
535: File.Move(name + "_vl.jpg", name_new + "_vl.jpg");
536: File.Move(name + "_l.jpg", name_new + "_l.jpg");
537: File.Move(name + "_m.jpg", name_new + "_m.jpg");
538: File.Move(name + "_s.jpg", name_new + "_s.jpg");
539: File.Move(name + "_t.jpg", name_new + "_t.jpg");
540: File.Move(name + ".jpg", name_new + ".jpg");
541:
542: File.Move(name + "_500.jpg", name_new + "_500.jpg");
543: File.Move(name + "_400.jpg", name_new + "_400.jpg");
544: File.Move(name + "_250.jpg", name_new + "_250.jpg");
545: File.Move(name + "_100.jpg", name_new + "_100.jpg");
546: File.Move(name + "_75.jpg", name_new + "_75.jpg");
547: }
548:
549: ////////////////////////////////////////////////////////////////////////////
550:
551: /// <summary>
552: ///
553: /// </summary>
554: public static int CountGeneralUse(string relative_path, int num)
555: {
556: int count;
557: string absolutePath, r;
558: string[] p;
559:
560: count = 0;
561: absolutePath = AbsolutePath();
562:
563: try
564: {
565: // count images:
566: p = Directory.GetFiles(absolutePath + relative_path, num + @"_*_t.jpg");
567:
568: count = p.Length;
569: }
570: catch (Exception ex)
571: {
572: #if DEBUG
573: r = "Error: " + ex.ToString();
574: #else
575: r = "Error: " + ex.Message;
576: #endif
577: }
578: finally
579: {
580: }
581:
582: return count;
583: }
584:
585: ////////////////////////////////////////////////////////////////////////////
586:
587: /// <summary>
588: ///
589: /// </summary>
590: private static bool ThumbnailCallback()
591: {
592: return false;
593: }
594:
595: #if WFA
596: #else
597: ////////////////////////////////////////////////////////////////////////////
598: ////////////////////////////////////////////////////////////////////////////
599:
600: /// <summary>
601: ///
602: /// </summary>
603: public static string Default(string file)
604: {
605: string line;
606:
607: try
608: {
609: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
610: line = "<img src=\"" + file + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" border=0>";
611: image.Dispose();
612: }
613: catch
614: {
615: line = ""; //" ";
616: }
617:
618: return line;
619: }
620:
621: ////////////////////////////////////////////////////////////////////////////
622:
623: /// <summary>
624: ///
625: /// </summary>
626: public static string Url(string file, string url)
627: {
628: string line;
629:
630: try
631: {
632: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
633: line = "<a href=" + url + "><img src=\"" + file + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" border=0></a>";
634: image.Dispose();
635: }
636: catch
637: {
638: line = ""; //" ";
639: }
640:
641: return line;
642: }
643:
644: ////////////////////////////////////////////////////////////////////////////
645:
646: /// <summary>
647: ///
648: /// </summary>
649: public static string Shadowed(string file)
650: {
651: // produce an image with nice shadow for a ltr document:
652: string line;
653:
654: try
655: {
656: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
657:
658: line = "<table cellpadding=0 cellspacing=0><tr><td><img src=\"" + file + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" border=0/></td><td width=5 height=5 valign=top background=image/shadow_ver_ltr.jpg><img src=image/shadow_ver_top_ltr.jpg width=5 height=5></td></tr><tr><td background=image/shadow_hor_ltr.jpg align=left><img src=image/shadow_hor_left_ltr.jpg width=5 height=5></td><td width=5 height=5><img src=image/shadow_corner_ltr.jpg width=5 height=5></td></tr></table>";
659:
660: /*
661: <table cellpadding="0" cellspacing="0">
662: <tr>
663: <td style="width:5px;height:5;vertical-align:top;background-image:url(../image/shadow_ver.jpg)"><img src="../image/shadow_ver_top.jpg" style="width:5px;height:5" alt=""></td>
664: <td><asp:HyperLink id="photo_hl" runat="server"/></td>
665: </tr>
666: <tr>
667: <td style="width:5px;height:5"><img src="../image/shadow_corner.jpg" style="width:5px;height:5" alt=""></td>
668: <td style="text-align:right;background-image:url(../image/shadow_hor.jpg)"><img src="../image/shadow_hor_left.jpg" style="width:5px;height:5" alt=""></td>
669: </tr>
670: </table>
671:
672: */
673:
674: /*
675: line = "<div id=\"image_shadowed\" style=\"position:relative\">"+
676: "<img src=\""+file+"\" style=\"position:absolute:left:0px;top:0px;width:"+image.Width+"px;height:"+image.Height+"px\"/>"+
677: "<img src=image/shadow_ver_top_ltr.jpg style=\"position:absolute;left:"+image.Width+"px;top:0px;width:5px;height:5px\"/>"+
678: "<img style=\"position:absolute;left:"+image.Width+"px;top:5px;width:5px;height:"+(image.Height-5)+"px;background-image:url(image/shadow_ver_ltr.jpg)\"/>"+
679: "<img src=image/shadow_hor_left_ltr.jpg style=\"position:absolute;left:0px;top:"+image.Height+"px;width:5px;height:5px\"/>"+
680: "<img style=\"position:absolute;left:5px;top:"+image.Height+";width:"+(image.Width-5)+"px;height:5px;background-image:url(image/shadow_hor_ltr.jpg)\"/>"+
681: "<img src=image/shadow_corner_ltr.jpg style=\"position:absolute;left:"+image.Width+"px;top:"+image.Height+"px;width:5px;height:5px\"/>"+
682: "</div>";
683:
684: */
685:
686: image.Dispose();
687: }
688: catch
689: {
690: line = ""; //" ";
691: }
692:
693: return line;
694: }
695:
696: ////////////////////////////////////////////////////////////////////////////
697:
698: /// <summary>
699: /// Add shadows to image. This does not display well on Chrome
700: /// </summary>
701: public static string ShadowedUrl(System.Web.UI.Page page, string file, string url)
702: {
703: string line;
704:
705: try
706: {
707: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
708:
709: line = "<table class=shadow cellpadding=0 cellspacing=0><tr><td><a href=" + url + "><img src=\"" + Ia.Cl.Model.Default.AbsolutePathUrl(page, file) + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" border=0/></a></td><td width=5 height=5 valign=top background=" + page.ResolveClientUrl("~/image/shadow_ver_ltr.jpg") + "><img src=" + page.ResolveClientUrl("~/image/shadow_ver_top_ltr.jpg") + " width=5 height=5></td></tr>" +
710: "<tr><td background=" + page.ResolveClientUrl("~/image/shadow_horizontal.jpg") + " align=right><img src=" + page.ResolveClientUrl("~/image/shadow_hor_left_ltr.jpg") + " width=5 height=5></td><td width=5 height=5><img src=" + page.ResolveClientUrl("~/image/shadow_corner_ltr.jpg") + " width=5 height=5></td></tr></table>";
711:
712: image.Dispose();
713: }
714: catch
715: {
716: line = ""; //" ";
717: }
718:
719: return line;
720: }
721:
722: ////////////////////////////////////////////////////////////////////////////
723:
724: /// <summary>
725: ///
726: /// </summary>
727: public static string TransparentPng(System.Web.UI.Page p, string file)
728: {
729: // produce div with a transparent PNG part showing
730: string line, image_url;
731:
732: image_url = Ia.Cl.Model.Default.AbsoluteUrl(p) + file;
733:
734: try
735: {
736: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
737: line = "<div style=\"width:" + image.Width + ";height:" + image.Height + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image_url + "');\"><img src=\"" + image_url + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" style=\"filter:Alpha(opacity=0)\"/></div>";
738: image.Dispose();
739: }
740: catch
741: {
742: line = ""; //" ";
743: }
744:
745: return line;
746: }
747:
748: ////////////////////////////////////////////////////////////////////////////
749:
750: /// <summary>
751: ///
752: /// </summary>
753: public static string TransparentPngShadowedUrl(System.Web.UI.Page p, string file, string navigate_url)
754: {
755: // produce div with a transparent PNG part showing and shadowed borders
756: string line, image_url;
757:
758: image_url = Ia.Cl.Model.Default.AbsoluteUrl(p) + file;
759:
760: try
761: {
762: System.Drawing.Image image = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/" + file));
763: line = "<div style=\"width:" + image.Width + ";height:" + image.Height + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image_url + "');\"><img src=\"" + image_url + "\" width=\"" + image.Width + "\" height=\"" + image.Height + "\" style=\"filter:Alpha(opacity=0)\"/></div>";
764: line = "<table cellpadding=0 cellspacing=0><tr><td style=\"cursor:hand;\"><a href=" + navigate_url + ">" + line + " </a></td><td width=5 height=5 valign=top background=image/shadow_ver_ltr.jpg><img src=image/shadow_ver_top_ltr.jpg width=5 height=5></td></tr><tr><td background=image/shadow_hor_ltr.jpg align=left><img src=image/shadow_hor_left_ltr.jpg width=5 height=5></td><td width=5 height=5><img src=image/shadow_corner_ltr.jpg width=5 height=5></td></tr></table>";
765: image.Dispose();
766: }
767: catch
768: {
769: line = ""; //" ";
770: }
771:
772: return line;
773: }
774: #endif
775:
776: /*
777: ////////////////////////////////////////////////////////////////////////////
778:
779: /// <summary>
780: ///
781: /// </summary>
782: private static void Image_Rotate_Prepare(XmlNode xn)
783: {
784: // prepare images read from the folder
785: //bool b;
786: int i;
787: string original, destination;
788: string source;
789:
790: i = 0;
791:
792: foreach (XmlNode n in xn.SelectNodes("* /rotator/group"))
793: {
794: original = n.ParentNode.SelectSingleNode("original").Attributes["folder"].Value;
795: destination = n.ParentNode.SelectSingleNode("destination").Attributes["folder"].Value;
796:
797: foreach (XmlNode o in n.SelectNodes("item"))
798: {
799: source = o.SelectSingleNode("source").InnerText;
800:
801: CreateGeneralUseImages(original + source, destination + i++);
802: }
803: }
804: }
805:
806: ////////////////////////////////////////////////////////////////////////////
807:
808: /// <summary>
809: ///
810: /// </summary>
811: public string Image_Rotate(string s_xml, string group_name, string size, int life_minute, string css_class)
812: {
813: int id, id_start;
814: string source, destination, url, alternate_text, extension, s, t;
815: StringBuilder sb;
816: Hashtable ht;
817: XmlNode xn;
818:
819: id = 0;
820: sb = new StringBuilder(1000);
821: ht = new Hashtable(100);
822:
823: id_start = -1;
824: destination = null;
825:
826: xn = xml.ReturnXmlNode(s_xml);
827:
828: try
829: {
830: foreach (XmlNode n in xn.SelectNodes("* /rotator/group/item"))
831: {
832: if (n.ParentNode.Attributes["name"].Value == group_name)
833: {
834: if (id_start == -1) id_start = id;
835:
836: source = n.SelectSingleNode("source").InnerText;
837: destination = n.ParentNode.ParentNode.SelectSingleNode("destination").Attributes["folder"].Value;
838:
839: extension = System.IO.Path.GetExtension(source);
840: extension = extension.ToLower();
841:
842: if (size == "large") s = "l";
843: else if (size == "middle") s = "m";
844: else s = "m";
845:
846: alternate_text = n.SelectSingleNode("alternate_text").InnerText;
847:
848: t = destination + id + "_" + s + extension + "|" + alternate_text;
849:
850: // change system path format to url path format
851: t = "~/" + t.Replace(@"\", @"/");
852: ht[id] = t;
853: }
854:
855: id++;
856: }
857:
858: id = ((DateTime.UtcNow.Minute + 1) / life_minute) % ht.Count + id_start;
859:
860: if (destination != null)
861: {
862: // generate images if there are non
863: if (Ia.Cl.Model.File.Count(destination, "*") == 0) Image_Rotate_Prepare(xn);
864: }
865:
866: // id = Ia.Cl.Model.Default.Random(ht.Count-1);
867:
868: url = ht[id].ToString().Split('|')[0];
869: alternate_text = ht[id].ToString().Split('|')[1];
870:
871: sb.Append("<span class=\"" + css_class + "\">");
872: sb.Append("<table>");
873: sb.Append("<tr><td><img src=" + url + " alt=" + alternate_text + "/></td></tr>");
874: sb.Append("<tr><td>" + alternate_text + "</td></tr>");
875: sb.Append("</table>");
876: sb.Append("</span>");
877: }
878: catch (Exception) { }
879:
880: return sb.ToString();
881: }
882: */
883:
884:
885: ////////////////////////////////////////////////////////////////////////////
886:
887: /// <summary>
888: /// Compare two bitmap images and return true if they match
889: /// </summary>
890: public static bool Compare(Bitmap bmp1, Bitmap bmp2)
891: {
892: // http://www.codeproject.com/KB/GDI-plus/comparingimages.aspx
893:
894: bool b;
895:
896: b = true;
897:
898: // test to see if we have the same size of image
899: if (bmp1.Size != bmp2.Size) b = false;
900: else
901: {
902: // convert each image to a byte array
903:
904: System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();
905:
906: byte[] btImage1 = new byte[1];
907: btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());
908:
909: byte[] btImage2 = new byte[1];
910: btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());
911:
912: // compute a hash for each image
913: SHA256Managed shaM = new SHA256Managed();
914: byte[] hash1 = shaM.ComputeHash(btImage1);
915: byte[] hash2 = shaM.ComputeHash(btImage2);
916:
917: // compare the hash values
918: for (int i = 0; i < hash1.Length && i < hash2.Length && b; i++) if (hash1[i] != hash2[i]) b = false;
919: }
920:
921: return b;
922: }
923:
924: ////////////////////////////////////////////////////////////////////////////
925:
926: /// <summary>
927: /// Check
928: /// </summary>
929: private static string AbsolutePath(string relativeOrAbsolutePath)
930: {
931: string path;
932:
933: if (relativeOrAbsolutePath.Contains(":"))
934: {
935: // this is an absolute path and we will return it
936: path = relativeOrAbsolutePath;
937: }
938: else
939: {
940: // this is a relative path and we will add to it the absolute path
941: path = AbsolutePath() + relativeOrAbsolutePath;
942: }
943:
944: return path;
945: }
946:
947: ////////////////////////////////////////////////////////////////////////////
948:
949: /// <summary>
950: /// Return the absolute path
951: /// </summary>
952: private static string AbsolutePath()
953: {
954: string path;
955:
956: #if WFA
957: if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + @"\";
958: else path = AppDomain.CurrentDomain.BaseDirectory;
959: #else
960: path = AppDomain.CurrentDomain.BaseDirectory.ToString();
961: #endif
962:
963: return path;
964: }
965:
966: ////////////////////////////////////////////////////////////////////////////
967:
968: /// <summary>
969: /// Resize the image to the specified width and height.
970: /// </summary>
971: /// <param name="image">The image to resize.</param>
972: /// <param name="width">The width to resize to.</param>
973: /// <param name="height">The height to resize to.</param>
974: /// <remarks>http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp</remarks>
975: /// <returns>The resized image.</returns>
976: public static Bitmap Resize(System.Drawing.Image image, int width, int height)
977: {
978: var destRect = new Rectangle(0, 0, width, height);
979: var destImage = new Bitmap(width, height);
980:
981: destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
982:
983: using (var graphics = Graphics.FromImage(destImage))
984: {
985: graphics.CompositingMode = CompositingMode.SourceCopy;
986: graphics.CompositingQuality = CompositingQuality.HighQuality;
987: graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
988: graphics.SmoothingMode = SmoothingMode.HighQuality;
989: graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
990:
991: using (var wrapMode = new ImageAttributes())
992: {
993: wrapMode.SetWrapMode(WrapMode.TileFlipXY);
994: graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
995: }
996: }
997:
998: return destImage;
999: }
1000:
1001: ////////////////////////////////////////////////////////////////////////////
1002:
1003: /// <summary>
1004: ///
1005: /// </summary>
1006: public static string ImageUrlDataFormat(Bitmap bitmap)
1007: {
1008: return string.Format("data:image/png;base64,{0}", Convert.ToBase64String(BitmapToBytes(bitmap)));
1009: }
1010:
1011: ////////////////////////////////////////////////////////////////////////////
1012:
1013: /// <summary>
1014: ///
1015: /// </summary>
1016: private static Byte[] BitmapToBytes(Bitmap bitmap)
1017: {
1018: using (MemoryStream memoryStream = new MemoryStream())
1019: {
1020: bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
1021:
1022: return memoryStream.ToArray();
1023: }
1024: }
1025:
1026: ////////////////////////////////////////////////////////////////////////////
1027: ////////////////////////////////////////////////////////////////////////////
1028: }
1029: }