/// <summary> /// Create a <see cref="NSImage"/> from a file. /// </summary> /// <param name="filename">The filename.</param> /// <returns>An autoreleased <see cref="NSImage"/> instance</returns> public static NSImage ImageFromFile(NSString filename) { NSImage result = null; if (filename != null) { NSData data = NSData.DataWithContentsOfFile(filename); result = new NSImage(data); result.Autorelease(); } return result; }
/// <summary> /// Create a <see cref="NSImage"/> from a stream. /// </summary> /// <param name="stream">The stream.</param> /// <returns>An autoreleased <see cref="NSImage"/> instance</returns> public static NSImage ImageFromStream(Stream stream) { NSImage result = null; if (stream != null) { byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); NSData data = new NSData(buffer); result = new NSImage(data); data.Release(); result.Autorelease(); } return result; }
public NSImage Snapshot(NSRect sourceRect) { NSImage snapshot = new NSImage(sourceRect.size); this.LockFocus(); NSBitmapImageRep rep = new NSBitmapImageRep(sourceRect).SafeAutorelease(); this.UnlockFocus(); snapshot.AddRepresentation(rep); snapshot.Autorelease(); return snapshot; }
/// <summary> /// Returns an autoreleased NSImage, consisting of the selected portion of the reciever's image. /// If there's no selection, this method will return the original image. /// </summary> public NSImage CroppedImage() { NSRect sourceImageRect = RectCoveredByImageInBounds(this.Cell.CastTo<NSImageCell>(), this.Bounds); NSRect newImageBounds = NSRect.NSIntersectionRect(this.selectionMarker.SelectedRect, sourceImageRect); if (!NSRect.NSIsEmptyRect(newImageBounds)) { NSImage newImage = new NSImage(sourceImageRect.size); NSAffineTransform pathAdjustment = NSAffineTransform.Transform; NSBezierPath croppingPath = this.selectionMarker.SelectedPath; pathAdjustment.TranslateXByYBy(-NSRect.NSMinX(sourceImageRect), -NSRect.NSMinY(sourceImageRect)); croppingPath = pathAdjustment.TransformBezierPath(croppingPath); newImage.LockFocus(); NSGraphicsContext.CurrentContext.ShouldAntialias = this.shouldAntiAlias; NSColor.BlackColor.Set(); croppingPath.Fill(); this.Image.CompositeToPointOperation(NSPoint.NSZeroPoint, NSCompositingOperation.NSCompositeSourceIn); newImage.UnlockFocus(); newImage.Autorelease(); return newImage; } return this.Image; }