/// <summary> /// Returns bitmap resized and cropped so that both height and width are the same /// </summary> /// <param name="SourceBitmap">Bitmap you want to scale to square</param> /// <param name="SideSize">Size of result bitmap in pixels</param> /// <param name="CropMode">What start point to take when cropping resized image to square</param> /// <returns>Sqare scaled and cropped bitmap</returns> public static Bitmap ResizeToSquare(this Bitmap SourceBitmap, int SideSize, CropPosition CropMode = CropPosition.LeftOrTop) { Bitmap _WorkingBitmap; Rectangle _Rect; int _WidthCropPosition; int _HeightCropPosition; _WidthCropPosition = 0; _HeightCropPosition = 0; if (SourceBitmap.Height < SourceBitmap.Width) { _WorkingBitmap = ResizeByHeight(SourceBitmap, SideSize); switch (CropMode) { case CropPosition.Center: _WidthCropPosition = (_WorkingBitmap.Width - SideSize) / 2; break; case CropPosition.LeftOrTop: _WidthCropPosition = 0; break; case CropPosition.RightOrBottom: _WidthCropPosition = _WorkingBitmap.Width - SideSize; break; default: _WidthCropPosition = 0; break; } } else { _WorkingBitmap = ResizeByWidth(SourceBitmap, SideSize); switch (CropMode) { case CropPosition.Center: _HeightCropPosition = (_WorkingBitmap.Height - SideSize) / 2; break; case CropPosition.LeftOrTop: _HeightCropPosition = 0; break; case CropPosition.RightOrBottom: _HeightCropPosition = _WorkingBitmap.Height - SideSize; break; default: _HeightCropPosition = 0; break; } } _Rect = new Rectangle(_WidthCropPosition, _HeightCropPosition, SideSize, SideSize); _WorkingBitmap = _WorkingBitmap.Clone(_Rect, _WorkingBitmap.PixelFormat); return(_WorkingBitmap); }
/// <summary> /// resize and crop to a specific location /// </summary> public void CropResize(float width, float height, CropPosition position) { SizeF ImgSize = modifiedImage.Size; // if (ImgSize.Width < width) width = ImgSize.Width; if (ImgSize.Height < height) height = ImgSize.Height; // float crop_x = 0; float crop_y = 0; if (ImgSize.Width / width < ImgSize.Height / height) { //scad din width RatioResizeToWidth (width); ImgSize = modifiedImage.Size; //compute crop_y if (position == CropPosition.Center) crop_y = (ImgSize.Height / 2) - (height / 2); if (position == CropPosition.End) crop_y = ImgSize.Height - height; } else { //change height RatioResizeToHeight (height); ImgSize = modifiedImage.Size; //calculeaza crop_x if (position == CropPosition.Center) crop_x = (ImgSize.Width / 2) - (width / 2); if (position == CropPosition.End) crop_x = ImgSize.Width - width; } //create new contect UIGraphics.BeginImageContext (new SizeF (width, height)); CGContext context = UIGraphics.GetCurrentContext (); //crops the new context to the desired height and width RectangleF clippedRect = new RectangleF (0, 0, width, height); context.ClipToRect (clippedRect); //draw my image on the context RectangleF drawRect = new RectangleF (-crop_x, -crop_y, ImgSize.Width, ImgSize.Height); modifiedImage.Draw (drawRect); //save the context in modifiedImage modifiedImage = UIGraphics.GetImageFromCurrentImageContext (); //close the context UIGraphics.EndImageContext (); }
/// <summary> /// Crops the specified image. /// </summary> /// <param name="image">The image.</param> /// <param name="cropMode">The crop mode.</param> /// <param name="aspectRatio">The aspect ratio.</param> /// <returns></returns> public static Image Crop(Image image, CropPosition cropMode, decimal aspectRatio) { if (aspectRatio == 0) { throw new ArgumentException("Aspect ratio must not be 0"); } aspectRatio = Math.Abs(aspectRatio); int width = 0 , height = 0 , posX = 0 , posY = 0; if (image.Width >= image.Height) { if (aspectRatio >= 1) { height = image.Height; width = (int)((decimal)image.Height / aspectRatio); } else { if (image.Width / 2 >= image.Height) { height = image.Height; width = (int)((decimal)image.Width * aspectRatio); } else { height = (int)((decimal)image.Width * aspectRatio); width = image.Width; } } } else { if (aspectRatio >= 1) { width = image.Width; height = (int)((decimal)image.Width / aspectRatio); } else { if (image.Height / 2 >= image.Width) { width = image.Width; height = (int)((decimal)image.Height * aspectRatio); } else { width = (int)((decimal)image.Height * aspectRatio); height = image.Height; } } } switch (cropMode) { case CropPosition.LeftOrTop: /* x -> 0, y -> 0 */ break; case CropPosition.Centered: if (image.Width >= image.Height) { if (image.Width / 2 >= image.Height) { // 4x1 // 1x1 if (height >= width / 2) { posX = image.Width / 2 - width / 2; } else if (width >= height / 2) { posY = image.Height / 2 - height / 2; } } else { // 4x3 if (height > width / 2) { posX = image.Width / 2 - width / 2; } else if (width > height / 2) { posY = image.Height / 2 - height / 2; } } } else { if (image.Height / 2 >= image.Width) { // 1x4 if (width >= height / 2) { posY = image.Height / 2 - height / 2; } else if (height >= width / 2) { posX = image.Width / 2 - width / 2; } } else { // 3x4 if (width > height / 2) { posY = image.Height / 2 - height / 2; } else if (height > width / 2) { posX = image.Width / 2 - width / 2; } } } break; case CropPosition.RightOrBottom: if (image.Width >= image.Height) { if (image.Width / 2 >= image.Height) { // 4x1 // 1x1 if (height >= width / 2) { posX = image.Width - width; } else if (width >= height / 2) { posY = image.Height - height; } } else { // 4x3 if (height > width / 2) { posX = image.Width - width; } else if (width > height / 2) { posY = image.Height - height; } } } else { if (image.Height / 2 >= image.Width) { // 1x4 if (width >= height / 2) { posY = image.Height - height; } else if (height >= width / 2) { posX = image.Width - width; } } else { // 3x4 if (width > height / 2) { posY = image.Height - height; } else if (height > width / 2) { posX = image.Width - width; } } } break; } Bitmap bmpImage = new Bitmap(image); Bitmap bmpCrop = bmpImage.Clone(new Rectangle(posX, posY, width, height), bmpImage.PixelFormat); return((Image)(bmpCrop)); }
public static UIImage CropResize(UIImage image, float width, float height, CropPosition position) { UIImage modifiedImage = image; SizeF ImgSize = modifiedImage.Size; if (ImgSize.Width < width) { width = ImgSize.Width; } if (ImgSize.Height < height) { height = ImgSize.Height; } float crop_x = 0; float crop_y = 0; if (ImgSize.Width / width < ImgSize.Height / height) { var cur_width = modifiedImage.Size.Width; if (cur_width > width) { var ratio = width / cur_width; var height2 = modifiedImage.Size.Height * ratio; var beforeResizeWidth = modifiedImage; UIGraphics.BeginImageContext(new SizeF(width, height2)); modifiedImage.Draw(new RectangleF(0, 0, width, height2)); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); beforeResizeWidth.Dispose(); } ImgSize = modifiedImage.Size; if (position == CropPosition.Center) { crop_y = (ImgSize.Height / 2) - (height / 2); } if (position == CropPosition.End) { crop_y = ImgSize.Height - height; } } else { var cur_height = modifiedImage.Size.Height; if (cur_height > height) { var ratio = height / cur_height; var width2 = modifiedImage.Size.Width * ratio; var beforeResizeHeight = modifiedImage; UIGraphics.BeginImageContext(new SizeF(width2, height)); modifiedImage.Draw(new RectangleF(0, 0, width2, height)); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); beforeResizeHeight.Dispose(); } ImgSize = modifiedImage.Size; if (position == CropPosition.Center) { crop_x = (ImgSize.Width / 2) - (width / 2); } if (position == CropPosition.End) { crop_x = ImgSize.Width - width; } } var beforeFinal = modifiedImage; UIGraphics.BeginImageContext(new SizeF(width, height)); CGContext context = UIGraphics.GetCurrentContext(); RectangleF clippedRect = new RectangleF(0, 0, width, height); context.ClipToRect(clippedRect); RectangleF drawRect = new RectangleF(-crop_x, -crop_y, ImgSize.Width, ImgSize.Height); modifiedImage.Draw(drawRect); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); context.Dispose(); beforeFinal.Dispose(); return(modifiedImage); }
public static UIImage CropResize(UIImage image, float width, float height, CropPosition position) { UIImage modifiedImage = image; SizeF ImgSize = modifiedImage.Size; if (ImgSize.Width < width) { width = ImgSize.Width; } if (ImgSize.Height < height) { height = ImgSize.Height; } float crop_x = 0; float crop_y = 0; if (ImgSize.Width / width < ImgSize.Height / height) { var cur_width = modifiedImage.Size.Width; if (cur_width > width) { var ratio = width / cur_width; var height2 = modifiedImage.Size.Height * ratio; var beforeResizeWidth = modifiedImage; UIGraphics.BeginImageContext (new SizeF (width, height2)); modifiedImage.Draw(new RectangleF (0, 0, width, height2)); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); beforeResizeWidth.Dispose(); } ImgSize = modifiedImage.Size; if (position == CropPosition.Center) { crop_y = (ImgSize.Height / 2) - (height / 2); } if (position == CropPosition.End) { crop_y = ImgSize.Height - height; } } else { var cur_height = modifiedImage.Size.Height; if (cur_height > height) { var ratio = height / cur_height; var width2 = modifiedImage.Size.Width * ratio; var beforeResizeHeight = modifiedImage; UIGraphics.BeginImageContext (new SizeF (width2, height)); modifiedImage.Draw(new RectangleF (0, 0, width2, height)); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext (); beforeResizeHeight.Dispose (); } ImgSize = modifiedImage.Size; if (position == CropPosition.Center) { crop_x = (ImgSize.Width / 2) - (width / 2); } if (position == CropPosition.End) { crop_x = ImgSize.Width - width; } } var beforeFinal = modifiedImage; UIGraphics.BeginImageContext (new SizeF (width, height)); CGContext context = UIGraphics.GetCurrentContext (); RectangleF clippedRect = new RectangleF (0, 0, width, height); context.ClipToRect (clippedRect); RectangleF drawRect = new RectangleF (-crop_x, -crop_y, ImgSize.Width, ImgSize.Height); modifiedImage.Draw (drawRect); modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); context.Dispose (); beforeFinal.Dispose(); return modifiedImage; }
/// <summary> /// resize and crop to a specific location /// </summary> public UIImage CropResize(float width, float height, CropPosition position) { SizeF ImgSize = modifiedImage.Size; // if (ImgSize.Width < width) { width = ImgSize.Width; } if (ImgSize.Height < height) { height = ImgSize.Height; } // float crop_x = 0; float crop_y = 0; if (ImgSize.Width / width < ImgSize.Height / height) { //scad din width RatioResizeToWidth(width); ImgSize = modifiedImage.Size; //compute crop_y if (position == CropPosition.Center) { crop_y = (ImgSize.Height / 2) - (height / 2); } if (position == CropPosition.End) { crop_y = ImgSize.Height - height; } } else { //change height RatioResizeToHeight(height); ImgSize = modifiedImage.Size; //calculeaza crop_x if (position == CropPosition.Center) { crop_x = (ImgSize.Width / 2) - (width / 2); } if (position == CropPosition.End) { crop_x = ImgSize.Width - width; } } //create new contect UIGraphics.BeginImageContext(new SizeF(width, height)); CGContext context = UIGraphics.GetCurrentContext(); //crops the new context to the desired height and width RectangleF clippedRect = new RectangleF(0, 0, width, height); context.ClipToRect(clippedRect); //draw my image on the context RectangleF drawRect = new RectangleF(-crop_x, -crop_y, ImgSize.Width, ImgSize.Height); modifiedImage.Draw(drawRect); //save the context in modifiedImage modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); //close the context UIGraphics.EndImageContext(); return(modifiedImage); }