Advertisement
cansik

MonoCompatibleImageManipulation

Jun 17th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5.  
  6. namespace ThatsMeSDK.Core
  7. {
  8.     class ImageManipulator
  9.     {
  10.  
  11.         /// <summary>
  12.         /// Creates a frame around an image
  13.         /// </summary>
  14.         /// <param name="image">Original image to frame</param>
  15.         /// <param name="width">Size of the frame</param>
  16.         /// <param name="color">Color of the frame</param>
  17.         /// <returns>Framed image</returns>
  18.         public Image FrameImage(Image image, int width, Color color)
  19.         {
  20.             Pen p = new Pen(new SolidBrush(color), width);
  21.             Graphics g = Graphics.FromImage(image);
  22.  
  23.             g.DrawRectangle(p, new Rectangle
  24.                 (0, 0, image.Size.Width - (width / 2), image.Size.Height - (width / 2)));
  25.             g.Dispose();
  26.  
  27.             return image;
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Cuts a rectangle out of an image
  32.         /// </summary>
  33.         /// <param name="image">Original image to crop</param>
  34.         /// <param name="cropArea">Aria to crop</param>
  35.         /// <returns>Cropped image</returns>
  36.         public Image CropImage(Image image, Rectangle cropArea)
  37.         {
  38.             Bitmap bmpCrop;
  39.             Bitmap bmpImage = new Bitmap(image);
  40.  
  41.             if (image.Width < cropArea.Width)
  42.             {
  43.                 cropArea.Width = image.Width;
  44.             }
  45.  
  46.             if (image.Height < cropArea.Height)
  47.             {
  48.                 cropArea.Height = image.Height;
  49.             }
  50.  
  51.             bmpCrop = bmpImage.Clone(cropArea,
  52.             bmpImage.PixelFormat);
  53.             return (Image)(bmpCrop);
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Resizes an image proportional
  58.         /// </summary>
  59.         /// <param name="image">Original image to resize</param>
  60.         /// <param name="size">Size of the new image (fit range)</param>
  61.         /// <param name="fillArea">If true, image fills the whole area</param>
  62.         /// <returns>Rezised image</returns>
  63.         public Image ResizeImage(Image image, Size size, bool fillArea)
  64.         {
  65.             int sourceWidth = image.Width;
  66.             int sourceHeight = image.Height;
  67.  
  68.             float nPercent = 0;
  69.             float nPercentW = 0;
  70.             float nPercentH = 0;
  71.  
  72.             nPercentW = ((float)size.Width / (float)sourceWidth);
  73.             nPercentH = ((float)size.Height / (float)sourceHeight);
  74.  
  75.             if (fillArea)
  76.             {
  77.                 if (nPercentH > nPercentW)
  78.                 {
  79.                     nPercent = nPercentH;
  80.                 }
  81.                 else
  82.                 {
  83.                     nPercent = nPercentW;
  84.                 }
  85.             }
  86.             else
  87.             {
  88.                 if (nPercentH < nPercentW)
  89.                 {
  90.                     nPercent = nPercentH;
  91.                 }
  92.                 else
  93.                 {
  94.                     nPercent = nPercentW;
  95.                 }
  96.             }
  97.  
  98.             int destWidth = (int)(sourceWidth * nPercent);
  99.             int destHeight = (int)(sourceHeight * nPercent);
  100.  
  101.             Bitmap b = new Bitmap(destWidth, destHeight);
  102.             Graphics g = Graphics.FromImage((Image)b);
  103.  
  104.             g.DrawImage(image, 0, 0, destWidth, destHeight);
  105.             g.Dispose();
  106.  
  107.             return (Image)b;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement