)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Image (Ia.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

Image processing support class.

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