diff --git a/articles/imagesharp/pixelbuffers.md b/articles/imagesharp/pixelbuffers.md index 87dc310d..37edb758 100644 --- a/articles/imagesharp/pixelbuffers.md +++ b/articles/imagesharp/pixelbuffers.md @@ -59,6 +59,29 @@ image.ProcessPixelRows(accessor => }); ``` +Need to process two images simultaneously? Sure! + +```C# +// Extract sub-region of sourceImage, bounded by sourceArea as a new image +private static Image Extract(Image sourceImage, Rectangle sourceArea) +{ + Image targetImage = new (sourceArea.Width, sourceArea.Height); + int height = sourceArea.Height; + sourceImage.ProcessPixelRows(targetImage, (sourceAccessor, targetAccessor) => + { + for (int i = 0; i < height; i++) + { + Span sourceRow = sourceAccessor.GetRowSpan(sourceArea.Y + i); + Span targetRow = targetAccessor.GetRowSpan(i); + + sourceRow.Slice(sourceArea.X, sourceArea.Width).CopyTo(targetRow); + } + }); + + return targetImage; +} +``` + ### Parallel, pixel-format agnostic image manipulation There is a way to process image data that is even faster than using the approach mentioned before, and that also has the advantage of working on images of any underlying pixel-format, in a completely transparent way: using the @"SixLabors.ImageSharp.Processing.PixelRowDelegateExtensions.ProcessPixelRowsAsVector4(SixLabors.ImageSharp.Processing.IImageProcessingContext,SixLabors.ImageSharp.Processing.PixelRowOperation)" APIs.