public NSScrubberItemView GetViewForItem(NSScrubber scrubber, nint index) { ScrubberImage item = (ScrubberImage)scrubber.MakeItem("com.xamarin.scrubber.image", null); // Tining each image causes lag, so cache and do off the main thread // Using ScrubberImage instead of NSScrubberImageItemView since // it does not handle setting Image later NSImage image; if (!imageCache.TryGetValue((int)index, out image)) { var template = (NSImage)Xamargon.Copy(); Task.Factory.StartNew(() => { var tintedImage = TintImage(template); BeginInvokeOnMainThread(() => { imageCache [(int)index] = tintedImage; item.Image = tintedImage; }); }); } else { item.Image = image; } // Put a nice border around it item.Layer.BorderColor = NSColor.DarkGray.CGColor; item.Layer.BorderWidth = 2; item.Layer.CornerRadius = 8; item.Layer.MasksToBounds = true; return(item); }
public static NSImage Tint(this NSImage image, NSColor color) { var copy = image.Copy() as NSImage; copy.LockFocus(); { color.Set(); var imageRect = new CGRect(CGPoint.Empty, image.Size); NSGraphics.RectFill(imageRect, NSCompositingOperation.SourceAtop); } copy.UnlockFocus(); return(copy); }
// Based on http://stackoverflow.com/a/43235 NSImage TintImage(NSImage image) { int r = random.Next(128, 255); int g = random.Next(128, 255); int b = random.Next(128, 255); NSColor baseColor = NSColor.White.UsingColorSpace(NSColorSpace.CalibratedRGB); r = (int)(r + baseColor.RedComponent) / 2; g = (int)(g + baseColor.GreenComponent) / 2; b = (int)(b + baseColor.BlueComponent) / 2; NSColor color = NSColor.FromRgb(r, g, b); NSImage tintedImage = (NSImage)image.Copy(); tintedImage.LockFocus(); color.Set(); NSGraphics.RectFill(new CGRect(0, 0, image.Size.Width, image.Size.Height), NSCompositingOperation.SourceAtop); tintedImage.UnlockFocus(); return(tintedImage); }
/// <summary> /// Combines two images into a new one, so that the "badge" image overlays the bottom-right quarter of the other image. /// </summary> /// <returns>The with badge.</returns> /// <param name="icon">Background of the new image</param> /// <param name="badge">Overlay image</param> internal static NSImage IconWithBadge(NSImage icon, NSImage badge) { if (icon == null) { return(null); } if (badge == null) { return(icon); } var result = (NSImage)icon.Copy(); result.LockFocus(); NSGraphicsContext.CurrentContext.ImageInterpolation = NSImageInterpolation.High; var dstRect = new CGRect(icon.Size.Width / 2, 0, icon.Size.Width / 2, icon.Size.Height / 2); var srcRect = new CGRect(0, 0, badge.Size.Width, badge.Size.Height); badge.DrawInRect(dstRect, srcRect, NSCompositingOperation.SourceOver, 1.0f); result.UnlockFocus(); return(result); }
public NSImage GetScreenshot() { if (View == null) { return(null); } NSImageView imageView = View.CastTo <NSImageView>(); if (imageView == null) { return(null); } NSImage image = imageView.Image; if (image != null) { return(image.Copy <NSImage>().Autorelease <NSImage>()); } return(null); }