public bool CreateThumbnail(CropFrom cropFrom = CropFrom.Center) { switch (this.imageSize.cropType) { case CropType.Square: return(Graphics.CreateSquareCroppedThumbnail(this.imageSize.height, this.paths.originalImage.paths.diskFullPath, this.paths.diskFullPathBase64, cropFrom)); case CropType.Panoramic: return(Graphics.CreatePanoramicThumbnail(this.imageSize.width, Graphics.panoramicAspectRatio, this.paths.originalImage.paths.diskFullPath, this.paths.diskFullPathBase64, cropFrom)); default: return(Graphics.CreateThumbnailToWidthHeight(this.imageSize.width, this.imageSize.height, this.paths.originalImage.paths.diskFullPath, this.paths.diskFullPathBase64)); } }
public static double panoramicAspectRatio = (double)0.675; /* height / width */ public static bool CreatePanoramicThumbnail(int width, double aspectRatio, string serverpath, string thumbpath, CropFrom cropFrom = CropFrom.Center) { try { if (!File.Exists(thumbpath)) { using (MagickImage image = new MagickImage(serverpath)) { /* first get crop shape */ int imageWidthToCropTo = image.Width; int imageHeightToCropTo; int requiredHeight = (int)(image.Width * aspectRatio); if (image.Height < requiredHeight) { imageWidthToCropTo = (int)(image.Height / aspectRatio); imageHeightToCropTo = image.Height; } else { imageHeightToCropTo = requiredHeight; } /* then get crop offsets */ int x = 0; int y = 0; switch (cropFrom) { case CropFrom.Center: x = (image.Width - imageWidthToCropTo) / 2; y = (image.Height - imageHeightToCropTo) / 2; break; case CropFrom.Bottom: x = (image.Width - imageWidthToCropTo) / 2; y = (image.Height - imageHeightToCropTo); break; case CropFrom.Top: x = (image.Width - imageWidthToCropTo) / 2; y = 0; break; case CropFrom.Left: x = 0; y = (image.Height - imageHeightToCropTo) / 2; break; case CropFrom.Right: x = (image.Width - imageWidthToCropTo); y = (image.Height - imageHeightToCropTo) / 2; break; } MagickGeometry size = new MagickGeometry(x, y, imageWidthToCropTo, imageHeightToCropTo); size.IgnoreAspectRatio = false; image.Crop(size); /* get final image at smaller size */ MagickGeometry finalsize = new MagickGeometry(0, 0, width, (int)(width * aspectRatio)); image.Resize(finalsize); image.Write(thumbpath); } } } catch (Exception) { return(false); } return(true); }
public static bool CreateSquareCroppedThumbnail(int dimension, string serverpath, string thumbpath, CropFrom cropFrom = CropFrom.Center) { try { if (!File.Exists(thumbpath)) { using (MagickImage image = new MagickImage(serverpath)) { // need to resize to roughly right size then crop int roughSize; int x = 0; int y = 0; if (image.Height < image.Width) { roughSize = (int)((double)dimension / image.Height * image.Width); } else { roughSize = (int)((double)dimension / image.Width * image.Height); } MagickGeometry roughsize = new MagickGeometry(roughSize + 3, roughSize + 3); image.Resize(roughsize); switch (cropFrom) { case CropFrom.Center: if (image.Height < image.Width) // center crop { x = (int)((image.Width - image.Height) / 2); } else { y = (int)((image.Height - image.Width) / 2); } break; case CropFrom.Bottom: if (image.Height > image.Width) { y = (int)(image.Height - image.Width); } break; case CropFrom.Top: // y = 0 break; case CropFrom.Left: // x = 0 break; case CropFrom.Right: if (image.Height < image.Width) { x = (int)(image.Width - image.Height); } break; } MagickGeometry size = new MagickGeometry(x, y, dimension, dimension); size.IgnoreAspectRatio = false; image.Crop(size); image.Write(thumbpath); } } } catch (Exception) { return(false); } return(true); }
/// <summary> /// Initializes a new instance of the <see cref="T:AGS.API.AGSCropInfo"/> struct. /// </summary> /// <param name="boundingBox">Bounding box.</param> /// <param name="textureBox">Texture box.</param> /// <param name="cropFrom">The direction the item was cropped from, if fully cropped.</param> public AGSCropInfo(AGSBoundingBox boundingBox, FourCorners <Vector2> textureBox, CropFrom cropFrom) { TextureBox = textureBox; BoundingBox = boundingBox; CropFrom = cropFrom; }