public static BitmapHolder ToTransformedCorners(BitmapHolder source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); //TODO not implemented return source; }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { _topLeftCornerSize = cornersSize; _topRightCornerSize = cornersSize; _bottomLeftCornerSize = cornersSize; _bottomRightCornerSize = cornersSize; _cornersTransformType = cornersTransformType; _cropWidthRatio = cropWidthRatio; _cropHeightRatio = cropHeightRatio; }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType) { _topLeftCornerSize = topLeftCornerSize; _topRightCornerSize = topRightCornerSize; _bottomLeftCornerSize = bottomLeftCornerSize; _bottomRightCornerSize = bottomRightCornerSize; _cornersTransformType = cornersTransformType; _cropWidthRatio = 1f; _cropHeightRatio = 1f; }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { TopLeftCornerSize = topLeftCornerSize; TopRightCornerSize = topRightCornerSize; BottomLeftCornerSize = bottomLeftCornerSize; BottomRightCornerSize = bottomRightCornerSize; CornersTransformType = cornersTransformType; CropWidthRatio = cropWidthRatio; CropHeightRatio = cropHeightRatio; }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType) : this(topLeftCornerSize, topRightCornerSize, bottomLeftCornerSize, bottomRightCornerSize, cornersTransformType, 1d, 1d) { }
private static bool HasFlag(CornerTransformType flags, CornerTransformType flag) { return (flags & flag) != 0; }
public static BitmapHolder ToTransformedCorners(BitmapHolder source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); double cropX = ((sourceWidth - desiredWidth) / 2); double cropY = ((sourceHeight - desiredHeight) / 2); BitmapHolder bitmap = null; if (cropX != 0 || cropY != 0) { bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight)); } else { bitmap = new BitmapHolder(source.Pixels, source.Width, source.Height); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; int topLeftSize = (int)topLeftCornerSize; int topRightSize = (int)topRightCornerSize; int bottomLeftSize = (int)bottomLeftCornerSize; int bottomRightSize = (int)bottomRightCornerSize; int w = bitmap.Width; int h = bitmap.Height; int transparentColor = Colors.Transparent.ToInt(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x <= topLeftSize && y <= topLeftSize) { //top left corner if (!CheckCorner(topLeftSize, topLeftSize, topLeftSize, cornersTransformType, Corner.TopLeftCorner, x, y)) bitmap.Pixels[y * w + x] = transparentColor; } else if (x >= w - topRightSize && y <= topRightSize && topRightSize > 0) { // top right corner if (!CheckCorner(w - topRightSize, topRightSize, topRightSize, cornersTransformType, Corner.TopRightCorner, x, y)) bitmap.Pixels[y * w + x] = transparentColor; } else if (x >= w - bottomRightSize && y >= h - bottomRightSize && bottomRightSize > 0) { // bottom right corner if (!CheckCorner(w - bottomRightSize, h - bottomRightSize, bottomRightSize, cornersTransformType, Corner.BottomRightCorner, x, y)) bitmap.Pixels[y * w + x] = transparentColor; } else if (x <= bottomLeftSize && y >= h - bottomLeftSize && bottomLeftSize > 0) { // bottom left corner if (!CheckCorner(bottomLeftSize, h - bottomLeftSize, bottomLeftSize, cornersTransformType, Corner.BottomLeftCorner, x, y)) bitmap.Pixels[y * w + x] = transparentColor; } } } return bitmap; }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { throw new Exception(DoNotReference); }
public static Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) using (var path = new Path()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(0, (float)topLeftCornerSize); path.LineTo((float)topLeftCornerSize, 0); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(0, (float)topLeftCornerSize); path.QuadTo(0, 0, (float)topLeftCornerSize, 0); } else { path.MoveTo(0, 0); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.LineTo((float)desiredWidth, (float)topRightCornerSize); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize); } else { path.LineTo((float)desiredWidth, 0); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else { path.LineTo((float)desiredWidth, (float)desiredHeight); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize)); } else { path.LineTo(0, (float)desiredHeight); } path.Close(); canvas.DrawPath(path, paint); return(bitmap); } }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType) { throw new Exception(Common.DoNotReferenceMessage); }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType) { Helpers.ThrowOrDefault(); }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType) { Helpers.ThrowOrDefault(); }
public static BitmapHolder ToTransformedCorners(BitmapHolder source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } double cropX = ((sourceWidth - desiredWidth) / 2); double cropY = ((sourceHeight - desiredHeight) / 2); BitmapHolder bitmap = null; if (cropX != 0 || cropY != 0) { bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight)); } else { bitmap = new BitmapHolder(source.PixelData, source.Width, source.Height); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; int topLeftSize = (int)topLeftCornerSize; int topRightSize = (int)topRightCornerSize; int bottomLeftSize = (int)bottomLeftCornerSize; int bottomRightSize = (int)bottomRightCornerSize; int w = bitmap.Width; int h = bitmap.Height; int transparentColor = Colors.Transparent.ToInt(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x <= topLeftSize && y <= topLeftSize) { //top left corner if (!CheckCorner(topLeftSize, topLeftSize, topLeftSize, cornersTransformType, Corner.TopLeftCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x >= w - topRightSize && y <= topRightSize && topRightSize > 0) { // top right corner if (!CheckCorner(w - topRightSize, topRightSize, topRightSize, cornersTransformType, Corner.TopRightCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x >= w - bottomRightSize && y >= h - bottomRightSize && bottomRightSize > 0) { // bottom right corner if (!CheckCorner(w - bottomRightSize, h - bottomRightSize, bottomRightSize, cornersTransformType, Corner.BottomRightCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x <= bottomLeftSize && y >= h - bottomLeftSize && bottomLeftSize > 0) { // bottom left corner if (!CheckCorner(bottomLeftSize, h - bottomLeftSize, bottomLeftSize, cornersTransformType, Corner.BottomLeftCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } } } return(bitmap); }
private static bool HasFlag(CornerTransformType flags, CornerTransformType flag) { return((flags & flag) != 0); }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType) { throw new Exception(Common.DoNotReferenceMessage); }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { Helpers.ThrowOrDefault(); }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) : this(cornersSize, cornersSize, cornersSize, cornersSize, cornersTransformType, cropWidthRatio, cropHeightRatio) { }
public CornersTransformation(double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { Helpers.ThrowOrDefault(); }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { throw new Exception(DoNotReference); }
public static UIImage ToTransformedCorners(UIImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { context.BeginPath(); using (var path = new UIBezierPath()) { // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddLineTo(new CGPoint(topLeftCornerSize, 0)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddQuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0)); } else { path.MoveTo(new CGPoint(0, 0)); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddLineTo(new CGPoint(desiredWidth, topRightCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0)); } else { path.AddLineTo(new CGPoint(desiredWidth ,0)); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddLineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight)); } else { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight)); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddLineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddQuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight)); } else { path.AddLineTo(new CGPoint(0, desiredHeight)); } path.ClosePath(); context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return modifiedImage; } } finally { UIGraphics.EndImageContext(); } }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType) { throw new Exception(DoNotReference); }
private static bool CheckCorner(int w, int h, int size, CornerTransformType flags, Corner which, int xC, int yC) { if ((HasFlag(flags, CornerTransformType.TopLeftCut) && which == Corner.TopLeftCorner) || (HasFlag(flags, CornerTransformType.TopRightCut) && which == Corner.TopRightCorner) || (HasFlag(flags, CornerTransformType.BottomRightCut) && which == Corner.BottomRightCorner) || (HasFlag(flags, CornerTransformType.BottomLeftCut) && which == Corner.BottomLeftCorner)) return CheckCutCorner(w, h, size, which, xC, yC); if ((HasFlag(flags, CornerTransformType.TopLeftRounded) && which == Corner.TopLeftCorner) || (HasFlag(flags, CornerTransformType.TopRightRounded) && which == Corner.TopRightCorner) || (HasFlag(flags, CornerTransformType.BottomRightRounded) && which == Corner.BottomRightCorner) || (HasFlag(flags, CornerTransformType.BottomLeftRounded) && which == Corner.BottomLeftCorner)) return CheckRoundedCorner(w, h, size, which, xC, yC); return true; }
public CornersTransformation(double cornersSize, CornerTransformType cornersTransformType) : this(cornersSize, cornersSize, cornersSize, cornersSize, cornersTransformType, 1d, 1d) { }
public static UIImage ToTransformedCorners(UIImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { context.BeginPath(); using (var path = new UIBezierPath()) { // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddLineTo(new CGPoint(topLeftCornerSize, 0)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddQuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0)); } else { path.MoveTo(new CGPoint(0, 0)); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddLineTo(new CGPoint(desiredWidth, topRightCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0)); } else { path.AddLineTo(new CGPoint(desiredWidth, 0)); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddLineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight)); } else { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight)); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddLineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddQuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight)); } else { path.AddLineTo(new CGPoint(0, desiredHeight)); } path.ClosePath(); context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return(modifiedImage); } } finally { UIGraphics.EndImageContext(); } }
public static NSImage ToTransformedCorners(NSImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.CGImage.Width; double sourceHeight = source.CGImage.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); var colorSpace = CGColorSpace.CreateDeviceRGB(); const int bytesPerPixel = 4; int width = (int)desiredWidth; int height = (int)desiredHeight; var bytes = new byte[width * height * bytesPerPixel]; int bytesPerRow = bytesPerPixel * width; const int bitsPerComponent = 8; using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big)) { context.BeginPath(); using (var path = new NSBezierPath()) { // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.LineTo(new CGPoint(topLeftCornerSize, 0)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.QuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0)); } else { path.MoveTo(new CGPoint(0, 0)); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.LineTo(new CGPoint(desiredWidth, topRightCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.QuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0)); } else { path.LineTo(new CGPoint(desiredWidth, 0)); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.LineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.QuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight)); } else { path.LineTo(new CGPoint(desiredWidth, desiredHeight)); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.LineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.QuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight)); } else { path.LineTo(new CGPoint(0, desiredHeight)); } path.ClosePath(); context.AddPath(path.ToCGPath()); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); context.DrawImage(drawRect, source.CGImage); using (var output = context.ToImage()) { return(new NSImage(output, CGSize.Empty)); } } }
public static Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) using (var path = new Path()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(0, (float)topLeftCornerSize); path.LineTo((float)topLeftCornerSize, 0); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(0, (float)topLeftCornerSize); path.QuadTo(0, 0, (float)topLeftCornerSize, 0); } else { path.MoveTo(0, 0); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.LineTo((float)desiredWidth, (float)topRightCornerSize); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize); } else { path.LineTo((float)desiredWidth ,0); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else { path.LineTo((float)desiredWidth, (float)desiredHeight); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize)); } else { path.LineTo(0, (float)desiredHeight); } path.Close(); canvas.DrawPath(path, paint); return bitmap; } }