/// <summary> /// Crop texture with params /// </summary> /// <param name="texture">Source texture</param> /// <param name="rect">Size and bounds</param> /// <param name="options">Crop option</param> /// <param name="x">Offset x for custom crop (begin from top right)</param> /// <param name="y">Offset y for custom crop (begin from top right)</param> /// <returns>Cropped textute</returns> public static Texture2D CropTexture(Texture2D texture, Vector2 crop, CropOptions options = CropOptions.CENTER, int x = 0, int y = 0) { if (crop.x < 0f || crop.y < 0f) { return(texture); } Rect sizes = new Rect(); Texture2D result = new Texture2D((int)crop.x, (int)crop.y); if (crop.x != 0f && crop.y != 0f) { sizes.x = 0; sizes.y = 0; sizes.width = crop.x; sizes.height = crop.y; switch (options) { case CropOptions.CENTER: sizes.x = (texture.width - crop.x) / 2f; sizes.y = (texture.height - crop.y) / 2f; break; case CropOptions.BOTTOM_RIGHT: sizes.x = texture.width - crop.x; break; case CropOptions.BOTTOM_LEFT: break; case CropOptions.TOP_LEFT: sizes.y = texture.height - crop.y; break; case CropOptions.TOP_RIGHT: sizes.x = texture.width - crop.x; sizes.y = texture.height - crop.y; break; case CropOptions.CUSTOM: float width = texture.width - crop.x - x; float height = texture.height - crop.y - y; sizes.x = (width > texture.width) ? 0f : width; sizes.y = (height > texture.height) ? 0f : height; break; } if ((texture.width < sizes.x + crop.x) || (texture.height < sizes.y + crop.y) || (sizes.x > texture.width) || (sizes.y > texture.height) || (sizes.x < 0) || (sizes.y < 0) || (crop.x < 0) || (crop.y < 0)) { return(texture); } result.SetPixels(texture.GetPixels(Mathf.FloorToInt(sizes.x), Mathf.FloorToInt(sizes.y), Mathf.FloorToInt(sizes.width), Mathf.FloorToInt(sizes.height))); result.Apply(); } return(result); }
public static Image CropImage(this Image b, CropOptions options) { var cropSize = new Size(options.CropWidth, options.CropHeight); if (cropSize.Width > b.Width || cropSize.Height > b.Height) { cropSize = new Size( cropSize.Width > b.Width ? b.Width : cropSize.Width, cropSize.Height > b.Height ? b.Height : cropSize.Height); } int x = 0, y = 0; switch (options.CropAlignment) { case Alignment.Middle_Left: case Alignment.Middle_Center: case Alignment.Middle_Right: y = ((b.Height / 2) - (cropSize.Height / 2)); break; case Alignment.Bottom_Left: case Alignment.Bottom_Center: case Alignment.Bottom_Right: y = (b.Height - cropSize.Height); break; } switch (options.CropAlignment) { case Alignment.Top_Center: case Alignment.Middle_Center: case Alignment.Bottom_Center: x = ((b.Width / 2) - (cropSize.Width / 2)); break; case Alignment.Top_Right: case Alignment.Middle_Right: case Alignment.Bottom_Right: x = b.Width - cropSize.Width; break; } var r = new Rectangle(new Point(x, y), cropSize); Image ret = new Bitmap(cropSize.Width, cropSize.Height); using (var g = Graphics.FromImage(ret)) g.DrawImage(b, new Rectangle(Point.Empty, cropSize), r, GraphicsUnit.Pixel); b.Dispose(); return(ret); }
public static ProcessResult <Clip[]> Apply(CropOptions options, params Clip[] clips) { var processedClips = new Clip[clips.Length]; var start = options.Lengths.Length > 1 ? options.Lengths[0] : 0; var duration = options.Lengths.Length > 1 ? options.Lengths[1] : options.Lengths[0]; var i = 0; foreach (var clip in clips) { processedClips[i++] = CropClip(clip, start, duration); } return(new ProcessResult <Clip[]>(processedClips)); }
public void Crop() { Texture2D source = Resources.Load <Sprite> ("image").texture; Texture2D tmp = new Texture2D(1, 1); tmp.LoadImage(source.EncodeToJPG()); CropOptions options = (CropOptions)_selectedCropOption; int width = int.Parse(CropWidthInput.text); int height = int.Parse(CropHeightInput.text); int x = int.Parse(CropOffsetXInput.text); int y = int.Parse(CropOffsetYInput.text); tmp = CropScale.CropTexture(tmp, new Vector2(width, height), options, x, y); ResultImage.sprite = Sprite.Create(tmp, new Rect(0f, 0f, tmp.width, tmp.height), new Vector2(0.5f, 0.5f)); }