Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- namespace ThatsMeSDK.Core
- {
- class ImageManipulator
- {
- /// <summary>
- /// Creates a frame around an image
- /// </summary>
- /// <param name="image">Original image to frame</param>
- /// <param name="width">Size of the frame</param>
- /// <param name="color">Color of the frame</param>
- /// <returns>Framed image</returns>
- public Bitmap FrameImage(Bitmap image, int width, Color color)
- {
- Pen p = new Pen(new SolidBrush(color), width);
- Graphics g = Graphics.FromImage(image);
- g.DrawRectangle(p, new Rectangle
- (0, 0, image.Size.Width - (width / 2), image.Size.Height - (width / 2)));
- g.Dispose();
- return image;
- }
- /// <summary>
- /// Cuts a rectangle out of an image
- /// </summary>
- /// <param name="image">Original image to crop</param>
- /// <param name="cropArea">Aria to crop</param>
- /// <returns>Cropped image</returns>
- public Bitmap CropImage(Bitmap image, Rectangle cropArea)
- {
- Bitmap bmpCrop;
- Bitmap bmpImage = new Bitmap(image);
- if (image.Width < cropArea.Width)
- {
- cropArea.Width = image.Width;
- }
- if (image.Height < cropArea.Height)
- {
- cropArea.Height = image.Height;
- }
- bmpCrop = bmpImage.Clone(cropArea,
- bmpImage.PixelFormat);
- return (Bitmap)(bmpCrop);
- }
- /// <summary>
- /// Resizes an image proportional
- /// </summary>
- /// <param name="image">Original image to resize</param>
- /// <param name="size">Size of the new image (fit range)</param>
- /// <param name="fillArea">If true, image fills the whole area</param>
- /// <returns>Rezised image</returns>
- public Bitmap ResizeImage(Bitmap image, Size size, bool fillArea)
- {
- int sourceWidth = image.Width;
- int sourceHeight = image.Height;
- float nPercent = 0;
- float nPercentW = 0;
- float nPercentH = 0;
- nPercentW = ((float)size.Width / (float)sourceWidth);
- nPercentH = ((float)size.Height / (float)sourceHeight);
- if (fillArea)
- {
- if (nPercentH > nPercentW)
- {
- nPercent = nPercentH;
- }
- else
- {
- nPercent = nPercentW;
- }
- }
- else
- {
- if (nPercentH < nPercentW)
- {
- nPercent = nPercentH;
- }
- else
- {
- nPercent = nPercentW;
- }
- }
- int destWidth = (int)(sourceWidth * nPercent);
- int destHeight = (int)(sourceHeight * nPercent);
- Bitmap b = new Bitmap(destWidth, destHeight);
- Graphics g = Graphics.FromImage((Image)b);
- g.DrawImage(image, 0, 0, destWidth, destHeight);
- g.Dispose();
- return (Bitmap)b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement