Ejemplo n.º 1
0
        private void GetBackgroundColor(IWICMetadataQueryReader wicMetadataQueryReader)
        {
            // 如果图片里面包含了 global palette 就需要获取 palette 和背景色
            var propertyVariant = new PROPVARIANT();

            wicMetadataQueryReader.GetMetadataByName("/logscrdesc/GlobalColorTableFlag", ref propertyVariant);

            byte backgroundIndex = 0;

            var globalPalette = (propertyVariant.Type & VARTYPE.VT_BOOL) == VARTYPE.VT_BOOL &&
                                propertyVariant.Value.UI1 > 0;

            if (globalPalette)
            {
                propertyVariant = new PROPVARIANT();
                wicMetadataQueryReader.GetMetadataByName("/logscrdesc/BackgroundColorIndex", ref propertyVariant);

                if ((propertyVariant.Type & VARTYPE.VT_UI1) == VARTYPE.VT_UI1)
                {
                    backgroundIndex = propertyVariant.Value.UI1;
                }

                var wicPalette = WicImagingFactory.CreatePalette();
                WicBitmapDecoder.CopyPalette(wicPalette);
            }
        }
        /// <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);
            }
        }
        public static bool TryGetMetadataByName(this IWICMetadataQueryReader meta, string name, out PropVariant val)
        {
            val = null;

            int hr = ProxyFunctions.GetMetadataByName(meta, name, IntPtr.Zero);

            if (hr >= 0)
            {
                val = new PropVariant();
                meta.GetMetadataByName(name, val);
            }
            return(hr >= 0);
        }
Ejemplo n.º 4
0
 public static IEnumerable <string> GetNamesRecursive(this IWICMetadataQueryReader reader)
 {
     foreach (var name in reader.GetNames())
     {
         var val = reader.GetMetadataByName(name);
         if (val is IWICMetadataQueryReader)
         {
             foreach (var childName in GetNamesRecursive((IWICMetadataQueryReader)val))
             {
                 yield return(name + childName);
             }
         }
         else
         {
             yield return(name);
         }
     }
 }
        public static bool TryGetMetadataByName <T>(this IWICMetadataQueryReader metadataQueryReader, string name, out T value)
        {
            var variant = new PROPVARIANT();

            try
            {
                metadataQueryReader.GetMetadataByName(name, ref variant);
                return(TryDecode(ref variant, out value));
            }
            catch (COMException ex) when(ex.ErrorCode == HResult.WINCODEC_ERR_PROPERTYNOTFOUND)
            {
                value = default(T);
                return(false);
            }
            finally
            {
                Dispose(ref variant);
            }
        }
Ejemplo n.º 6
0
        public static bool TryGetMetadataProperty <T>(this IWICMetadataQueryReader queryReader, string propertyPath, out T value)
        {
            if (queryReader == null)
            {
                throw new ArgumentNullException("queryReader");
            }
            value = default(T);
            PROPVARIANT propvariant    = default(PROPVARIANT);
            int         metadataByName = queryReader.GetMetadataByName(propertyPath, out propvariant);

            if (-2003292352 != metadataByName)
            {
                GraphicsInteropNativeMethods.CheckNativeResult(metadataByName);
                value = (T)((object)propvariant.Value);
                propvariant.Dispose();
                return(true);
            }
            propvariant.Dispose();
            return(false);
        }
        /// <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);
            }
        }