public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight) { UIImage newImage = null; if (null != img) { var w = img.Size.Width; var h = img.Size.Height; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGContext context = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst); context.BeginPath(); var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height); AddRoundedRectToPath(context, rect, cornerWidth, cornerHeight); context.ClosePath(); context.Clip(); var cgImage = img.CGImage; context.DrawImage(new RectangleF(0, 0, w, h), cgImage); cgImage.Dispose(); CGImage imageMasked = ((CGBitmapContext)context).ToImage(); context.Dispose(); colorSpace.Dispose(); newImage = new UIImage(imageMasked); imageMasked.Dispose(); } return(newImage); }
public static NSImage ToRounded(NSImage source, nfloat rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor) { 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); } float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); if (rad == 0) { rad = (nfloat)(Math.Min(desiredWidth, desiredHeight) / 2); } else { rad = (nfloat)(rad * (desiredWidth + desiredHeight) / 2 / 500); } 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)) { var clippedRect = new CGRect(0d, 0d, desiredWidth, desiredHeight); context.BeginPath(); using (var path = NSBezierPath.FromRoundedRect(clippedRect, rad, rad)) { context.AddPath(path.ToCGPath()); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); context.DrawImage(drawRect, source.CGImage); if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 1000d); var borderRect = new CGRect((0d + borderSize / 2d), (0d + borderSize / 2d), (desiredWidth - borderSize), (desiredHeight - borderSize)); context.BeginPath(); using (var path = NSBezierPath.FromRoundedRect(borderRect, rad, rad)) { context.SetStrokeColor(borderHexColor.ToUIColor().CGColor); context.SetLineWidth((nfloat)borderSize); context.AddPath(path.ToCGPath()); context.StrokePath(); } } using (var output = context.ToImage()) { return(new NSImage(output, CGSize.Empty)); } } }
public static UIImage MakeRoundCornerImage(UIImage img, int cornerWidth, int cornerHeight) { UIImage newImage = null; if (null != img) { var w = img.Size.Width; var h = img.Size.Height; CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); CGContext context = new CGBitmapContext(null, (int)w, (int)h, 8, (int)(4 * w), colorSpace, CGImageAlphaInfo.PremultipliedFirst); context.BeginPath(); var rect = new RectangleF(0, 0, img.Size.Width, img.Size.Height); AddRoundedRectToPath (context, rect, cornerWidth, cornerHeight); context.ClosePath(); context.Clip(); var cgImage = img.CGImage; context.DrawImage (new RectangleF(0, 0, w, h), cgImage); cgImage.Dispose(); CGImage imageMasked = ((CGBitmapContext)context).ToImage(); context.Dispose(); colorSpace.Dispose(); newImage = new UIImage(imageMasked); imageMasked.Dispose(); } return newImage; }
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 NSImage ToCropped(NSImage source, double zoomFactor, double xOffset, double yOffset, 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); } xOffset = xOffset * desiredWidth; yOffset = yOffset * desiredHeight; desiredWidth = desiredWidth / zoomFactor; desiredHeight = desiredHeight / zoomFactor; float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset); float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset); if (cropX < 0) { cropX = 0; } if (cropY < 0) { cropY = 0; } if (cropX + desiredWidth > sourceWidth) { cropX = (float)(sourceWidth - desiredWidth); } if (cropY + desiredHeight > sourceHeight) { cropY = (float)(sourceHeight - desiredHeight); } 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)) { var clippedRect = new CGRect(0, 0, desiredWidth, desiredHeight); context.BeginPath(); using (var path = NSBezierPath.FromRect(clippedRect)) { 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)); } } }