public static NSImage ToSolidColor(NSImage imageSource, int r, int g, int b, int a) { CGRect drawRect = new CGRect(0d, 0d, imageSource.CGImage.Width, imageSource.CGImage.Height); var colorSpace = CGColorSpace.CreateDeviceRGB(); const int bytesPerPixel = 4; int width = (int)imageSource.CGImage.Width; int height = (int)imageSource.CGImage.Height; 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.TranslateCTM(0, drawRect.Size.Height); context.ScaleCTM(1.0f, -1.0f); context.ClipToMask(drawRect, imageSource.CGImage); context.SetFillColor(new CGColor(r, g, b, a)); context.FillRect(drawRect); using (var output = context.ToImage()) { return(new NSImage(output, CGSize.Empty)); } } }
public static UIImage AdjustImage(CGRect rect, UIImage template, CGBlendMode mode, nfloat red, nfloat green, nfloat blue, nfloat alpha) { using (var cs = CGColorSpace.CreateDeviceRGB()) { using (var context = new CGBitmapContext(IntPtr.Zero, (int)rect.Width, (int)rect.Height, 8, (int)rect.Height * 4, cs, CGImageAlphaInfo.PremultipliedLast)) { context.TranslateCTM(0.0f, 0f); //context.ScaleCTM(1.0f,-1.0f); context.DrawImage(rect, template.CGImage); context.SetBlendMode(mode); context.ClipToMask(rect, template.CGImage); context.SetFillColor(red, green, blue, alpha); context.FillRect(rect); return(UIImage.FromImage(context.ToImage())); } } }
public static UIImage AdjustImage (RectangleF rect, UIImage template, CGBlendMode mode, float red, float green, float blue, float alpha) { using (var cs = CGColorSpace.CreateDeviceRGB ()) { using (var context = new CGBitmapContext (IntPtr.Zero, (int)rect.Width, (int)rect.Height, 8, (int)rect.Height * 4, cs, CGImageAlphaInfo.PremultipliedLast)) { context.TranslateCTM (0.0f, 0f); //context.ScaleCTM(1.0f,-1.0f); context.DrawImage (rect, template.CGImage); context.SetBlendMode (mode); context.ClipToMask (rect, template.CGImage); context.SetFillColor (red, green, blue, alpha); context.FillRect (rect); return UIImage.FromImage (context.ToImage ()); } } }
public static UIImage MaskeWithColor(this UIImage image, UIColor color) { var maskImage = image.CGImage; var width = image.Size.Width; var height = image.Size.Height; var bounds = new CGRect(0, 0, width, height); using (var colorSpace = CGColorSpace.CreateDeviceRGB()) { using (var bitmapContext = new CGBitmapContext(null, (nint)width, (nint)height, 8, 0, colorSpace, CGImageAlphaInfo.PremultipliedLast)) { bitmapContext.ClipToMask(bounds, maskImage); bitmapContext.SetFillColor(color.CGColor); bitmapContext.FillRect(bounds); using (var cImage = bitmapContext.ToImage()) { var coloredImage = UIImage.FromImage(cImage); return(coloredImage); } } } }