/// <summary>
        /// Converts the EXIF meta data to JPEG format.
        /// </summary>
        /// <param name="metaData">The meta data to convert.</param>
        /// <returns>The converted meta data or null.</returns>
        private static BitmapMetadata ConvertEXIFMetaData(BitmapMetadata metaData)
        {
            BitmapMetadata exifData = null;

            try
            {
                exifData = metaData.GetQuery("/ifd/exif") as BitmapMetadata;
            }
            catch (IOException)
            {
                // WINCODEC_ERR_INVALIDQUERYREQUEST
            }

            // Return null if the EXIF block does not contain any data.
            if ((exifData == null) || !exifData.Any())
            {
                return(null);
            }

            BitmapMetadata jpegMetaData = new BitmapMetadata("jpg");

            jpegMetaData.SetQuery("/app1/ifd/exif", new BitmapMetadata("exif"));

            foreach (var tag in exifData)
            {
                object         value   = exifData.GetQuery(tag);
                BitmapMetadata exifSub = value as BitmapMetadata;

                if (exifSub != null)
                {
                    CopySubBlockRecursive(ref jpegMetaData, exifSub, "/app1/ifd/exif" + tag);
                }
                else
                {
                    jpegMetaData.SetQuery("/app1/ifd/exif" + tag, value);
                }
            }

            // Set the fields that are relevant for EXIF.
            try
            {
                if (!string.IsNullOrEmpty(metaData.ApplicationName))
                {
                    jpegMetaData.ApplicationName = metaData.ApplicationName;
                }

                if (!string.IsNullOrEmpty(metaData.CameraManufacturer))
                {
                    jpegMetaData.CameraManufacturer = metaData.CameraManufacturer;
                }

                if (!string.IsNullOrEmpty(metaData.CameraModel))
                {
                    jpegMetaData.CameraModel = metaData.CameraModel;
                }
            }
            catch (NotSupportedException)
            {
            }

            return(jpegMetaData);
        }
        /// <summary>
        /// Converts the XMP meta data to TIFF format.
        /// </summary>
        /// <param name="metaData">The meta data to convert.</param>
        /// <param name="format">The format of the meta data.</param>
        /// <returns>The converted meta data or null.</returns>
        private static BitmapMetadata ConvertXMPMetaData(BitmapMetadata metaData, string format)
        {
            BitmapMetadata xmpData = null;

            try
            {
                if (format == "png")
                {
                    BitmapMetadata textChunk = metaData.GetQuery("/iTXt") as BitmapMetadata;

                    if (textChunk != null)
                    {
                        string keyWord = textChunk.GetQuery("/Keyword") as string;

                        if (keyWord == "XML:com.adobe.xmp")
                        {
                            string textEntry = textChunk.GetQuery("/TextEntry") as string;

                            if (!string.IsNullOrEmpty(textEntry))
                            {
                                xmpData = LoadPNGMetaData(textEntry);
                            }
                        }
                    }
                }
                else if (format == "jpg")
                {
                    xmpData = metaData.GetQuery("/xmp") as BitmapMetadata;
                }
                else
                {
                    try
                    {
                        xmpData = metaData.GetQuery("/ifd/xmp") as BitmapMetadata;
                    }
                    catch (IOException)
                    {
                        // WINCODEC_ERR_INVALIDQUERYREQUEST
                    }

                    if (xmpData == null)
                    {
                        // Some codecs may store the XMP data outside of the IFD block.
                        xmpData = metaData.GetQuery("/xmp") as BitmapMetadata;
                    }
                }
            }
            catch (IOException)
            {
                // WINCODEC_ERR_INVALIDQUERYREQUEST
            }

            // Return null if the XMP block does not contain any data.
            if ((xmpData == null) || !xmpData.Any())
            {
                return(null);
            }

            BitmapMetadata tiffMetaData = new BitmapMetadata("tiff");

            tiffMetaData.SetQuery("/ifd/xmp", new BitmapMetadata("xmp"));

            foreach (var tag in xmpData)
            {
                object         value  = xmpData.GetQuery(tag);
                BitmapMetadata xmpSub = value as BitmapMetadata;

                if (xmpSub != null)
                {
                    CopySubBlockRecursive(ref tiffMetaData, xmpSub, "/ifd/xmp" + tag);
                }
                else
                {
                    tiffMetaData.SetQuery("/ifd/xmp" + tag, value);
                }
            }

            return(tiffMetaData);
        }