From af33fd3026783c52a0c0f2f60a28adab95f4b689 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 19 Sep 2017 19:21:45 +0100 Subject: [PATCH] extract/clone frames --- src/ImageSharp/Image/Image{TPixel}.cs | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 5c35d854a3..e92255c208 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -148,6 +148,30 @@ namespace SixLabors.ImageSharp return new Image(this.configuration, this.MetaData.Clone(), frames); } + /// + /// Clones the current image + /// + /// The index of the frame to clone into the new image + /// Returns a new image with all the same metadata as the original but only the single frame. + public Image Clone(int frameIndex) + { + ImageFrame frame = this.frames[frameIndex]; + ImageFrame clonedFrame = frame.Clone(); + return new Image(this.configuration, this.MetaData.Clone(), new[] { clonedFrame }); + } + + /// + /// Extracts a frame from the current image + /// + /// The index of the frame to cloen into the new image + /// Returns a new image with all the same metadata as the original but only the single frame. + public Image Extract(int frameIndex) + { + ImageFrame frame = this.frames[frameIndex]; + this.frames.Remove(frame); // try and remove frame from the current image + return new Image(this.configuration, this.MetaData.Clone(), new[] { frame }); + } + /// public override string ToString() { @@ -168,6 +192,22 @@ namespace SixLabors.ImageSharp return target; } + /// + /// Returns a copy of the image in the given pixel format. + /// + /// The pixel format. + /// The index of the frame to clone into the new image + /// Returns a new with all the same metadata as the original but only the single frame. + public Image CloneAs(int frameIndex) + where TPixel2 : struct, IPixel + { + ImageFrame frame = this.frames[frameIndex]; + ImageFrame clonedFrame = frame.CloneAs(); + var target = new Image(this.configuration, this.MetaData, new[] { clonedFrame }); + + return target; + } + /// /// Releases managed resources. ///