public XmpDirectory Extract([NotNull] byte[] xmpBytes, int offset, int length)
        {
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), "Must be zero or greater.");
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), "Must be zero or greater.");
            }
            if (xmpBytes.Length < offset + length)
            {
                throw new ArgumentException("Extends beyond length of byte array.", nameof(length));
            }

            // Trim any trailing null bytes
            // https://github.com/drewnoakes/metadata-extractor-dotnet/issues/154
            while (xmpBytes[offset + length - 1] == 0)
            {
                length--;
            }

            var directory = new XmpDirectory();

            try
            {
                // Limit photoshop:DocumentAncestors node as it can reach over 100000 items and make parsing extremely slow.
                // This is not a typical value but it may happen https://forums.adobe.com/thread/2081839
                var parseOptions = new ParseOptions();
                parseOptions.SetXMPNodesToLimit(new Dictionary <string, int>()
                {
                    { "photoshop:DocumentAncestors", 1000 }
                });

                var xmpMeta = XmpMetaFactory.ParseFromBuffer(xmpBytes, offset, length, parseOptions);
                directory.SetXmpMeta(xmpMeta);
            }
            catch (XmpException e)
            {
                directory.AddError("Error processing XMP data: " + e.Message);
            }
            return(directory);
        }