/// <summary>
        /// Retrieves the metadata block or item identified by a metadata query expression.
        /// </summary>
        /// <param name="name">The query expression to the requested metadata block or item.</param>
        /// <param name="value">The metadata block or item requested or null if not found.</param>
        /// <returns>True when the metadata block or item was found, otherwise false.</returns>
        /// <remarks>
        /// GetMetadataByName uses metadata query expressions to access embedded metadata. For more information on the metadata query language, see the Metadata Query Language Overview.
        /// <br/>
        /// If multiple blocks or items exist that are expressed by the same query expression, the first metadata block or item found will be returned.
        /// </remarks>
        public static bool TryGetMetadataByName(this IWICMetadataQueryReader metadataQueryReader, string name, out object?value)
        {
            if (metadataQueryReader is null)
            {
                throw new NullReferenceException();
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var variant = new PROPVARIANT();

            try
            {
                metadataQueryReader.GetMetadataByName(name, ref variant);
                value = PropVariantHelper.Decode(variant);
                return(true);
            }
            catch (COMException ex) when(ex.ErrorCode == WinCodecError.PROPERTY_NOT_FOUND)
            {
                value = null;
                return(false);
            }
            finally
            {
                PropVariantHelper.Free(variant);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets a metadata item to a specific location.
        /// </summary>
        /// <param name="name">The name of the metadata item.</param>
        /// <param name="value">The metadata to set.</param>
        ///  <remarks>
        /// SetMetadataByName uses metadata query expressions to set metadata. For more information on the metadata query language, see the Metadata Query Language Overview.
        /// </remarks>
        public static void SetMetadataByName(this IWICMetadataQueryWriter metadataQueryWriter, string name, object value)
        {
            if (metadataQueryWriter is null)
            {
                throw new NullReferenceException();
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var variant = PropVariantHelper.Encode(value);

            try
            {
                metadataQueryWriter.SetMetadataByName(name, ref variant);
            }
            finally
            {
                PropVariantHelper.Free(variant);
            }
        }
        /// <summary>
        /// Retrieves the metadata block or item identified by a metadata query expression.
        /// </summary>
        /// <param name="name">The query expression to the requested metadata block or item.</param>
        /// <returns>The metadata block or item requested.</returns>
        /// <exception cref="COMException">Thrown when the metadata block or item was not found (HRESULT 0x88982F40).</exception>
        /// <remarks>
        /// GetMetadataByName uses metadata query expressions to access embedded metadata. For more information on the metadata query language, see the Metadata Query Language Overview.
        /// <br/>
        /// If multiple blocks or items exist that are expressed by the same query expression, the first metadata block or item found will be returned.
        /// </remarks>
        public static object GetMetadataByName(this IWICMetadataQueryReader metadataQueryReader, string name)
        {
            if (metadataQueryReader is null)
            {
                throw new NullReferenceException();
            }
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var variant = new PROPVARIANT();

            try
            {
                metadataQueryReader.GetMetadataByName(name, ref variant);
                return(PropVariantHelper.Decode(variant));
            }
            finally
            {
                PropVariantHelper.Free(variant);
            }
        }