Exemple #1
0
        private static AvifMetadata CreateAvifMetadata(Document doc)
        {
            byte[] exifBytes       = null;
            byte[] iccProfileBytes = null;
            byte[] xmpBytes        = null;

            Dictionary <MetadataKey, MetadataEntry> exifMetadata = GetExifMetadataFromDocument(doc);

            if (exifMetadata != null)
            {
                Exif.ExifColorSpace exifColorSpace = Exif.ExifColorSpace.Srgb;

                MetadataKey iccProfileKey = MetadataKeys.Image.InterColorProfile;

                if (exifMetadata.TryGetValue(iccProfileKey, out MetadataEntry iccProfileItem))
                {
                    iccProfileBytes = iccProfileItem.GetData();
                    exifMetadata.Remove(iccProfileKey);
                    exifColorSpace = Exif.ExifColorSpace.Uncalibrated;
                }

                exifBytes = new ExifWriter(doc, exifMetadata, exifColorSpace).CreateExifBlob();
            }

            XmpPacket xmpPacket = doc.Metadata.TryGetXmpPacket();

            if (xmpPacket != null)
            {
                string packetAsString = xmpPacket.ToString(XmpPacketWrapperType.ReadOnly);

                xmpBytes = System.Text.Encoding.UTF8.GetBytes(packetAsString);
            }

            return(new AvifMetadata(exifBytes, iccProfileBytes, xmpBytes));
        }
Exemple #2
0
        static void Main(string[] args)
        {
            // choose an image
            Console.Write("Enter image load path: ");
            string imagePath = Console.ReadLine();
            int    lastDot   = imagePath.LastIndexOf('.');

            Console.WriteLine();

            //----------------------------------------------

            // minimally loads image and closes it
            ExifPropertyCollection properties = ExifReader.GetExifData(imagePath);

            string dumpPath = imagePath.Substring(0, lastDot) + "_EXIF" + imagePath.Substring(lastDot) + ".txt";

            using (StreamWriter dumpWriter = File.CreateText(dumpPath))
            {
                // dump properties to console
                foreach (ExifProperty property in properties)
                {
                    dumpWriter.WriteLine("{0}.{1}: \"{2}\"",
                                         property.Tag.GetType().Name,
                                         property.Tag,
                                         property.DisplayName);
                    dumpWriter.WriteLine("{0}: {1}",
                                         GetPropertyTypeName(property.Value),
                                         property.Value);
                    dumpWriter.WriteLine("\"{0}\"",
                                         property.DisplayValue);
                    dumpWriter.WriteLine();
                }
            }

            Console.WriteLine();

            //----------------------------------------------
#if TEST
            string outputPath = imagePath.Substring(0, lastDot) + "_COPYRIGHT_LOREM_IPSUM" + imagePath.Substring(lastDot);
            Console.WriteLine("Adding dummy copyright to image and saving to:\r\n\t" + outputPath);

            // add copyright tag
            ExifProperty copyright = new ExifProperty();
            copyright.Tag   = ExifTag.Copyright;
            copyright.Value = String.Format(
                "Copyright (c){0} Lorem ipsum dolor sit amet. All rights reserved.",
                DateTime.Now.Year);

            ExifWriter.AddExifData(imagePath, outputPath, copyright);

            Console.WriteLine();

            //----------------------------------------------

            foreach (ExifTagOrientation i in Enum.GetValues(typeof(ExifTagOrientation)))
            {
                outputPath = imagePath.Substring(0, lastDot) + "_Orientation_" + (int)i + imagePath.Substring(lastDot);
                Console.WriteLine("Adding orientation to image and saving to:\r\n\t" + outputPath);

                // add orientation tag
                ExifProperty orientTag = new ExifProperty();
                orientTag.Tag   = ExifTag.Orientation;
                orientTag.Value = i;

                ExifWriter.AddExifData(imagePath, outputPath, orientTag);

                Console.WriteLine();
            }
#endif
        }
Exemple #3
0
        private static WebPNative.MetadataParams CreateWebPMetadata(Document doc)
        {
            byte[] iccProfileBytes = null;
            byte[] exifBytes       = null;
            byte[] xmpBytes        = null;

            string colorProfile = doc.Metadata.GetUserValue(WebPMetadataNames.ColorProfile);

            if (!string.IsNullOrEmpty(colorProfile))
            {
                iccProfileBytes = Convert.FromBase64String(colorProfile);
            }

            string exif = doc.Metadata.GetUserValue(WebPMetadataNames.EXIF);

            if (!string.IsNullOrEmpty(exif))
            {
                exifBytes = Convert.FromBase64String(exif);
            }

            string xmp = doc.Metadata.GetUserValue(WebPMetadataNames.XMP);

            if (!string.IsNullOrEmpty(xmp))
            {
                xmpBytes = Convert.FromBase64String(xmp);
            }

            if (iccProfileBytes == null || exifBytes == null)
            {
                Dictionary <MetadataKey, MetadataEntry> propertyItems = GetMetadataFromDocument(doc);

                if (propertyItems != null)
                {
                    ExifColorSpace exifColorSpace = ExifColorSpace.Srgb;

                    if (iccProfileBytes != null)
                    {
                        exifColorSpace = ExifColorSpace.Uncalibrated;
                    }
                    else
                    {
                        MetadataKey iccProfileKey = MetadataKeys.Image.InterColorProfile;

                        if (propertyItems.TryGetValue(iccProfileKey, out MetadataEntry iccProfileItem))
                        {
                            iccProfileBytes = iccProfileItem.GetData();
                            propertyItems.Remove(iccProfileKey);
                            exifColorSpace = ExifColorSpace.Uncalibrated;
                        }
                    }

                    if (exifBytes == null)
                    {
                        exifBytes = new ExifWriter(doc, propertyItems, exifColorSpace).CreateExifBlob();
                    }
                }
            }

#if !PDN_3_5_X
            if (xmpBytes == null)
            {
                PaintDotNet.Imaging.XmpPacket xmpPacket = doc.Metadata.TryGetXmpPacket();

                if (xmpPacket != null)
                {
                    string xmpPacketAsString = xmpPacket.ToString();

                    xmpBytes = System.Text.Encoding.UTF8.GetBytes(xmpPacketAsString);
                }
            }
#endif

            if (iccProfileBytes != null || exifBytes != null || xmpBytes != null)
            {
                return(new WebPNative.MetadataParams(iccProfileBytes, exifBytes, xmpBytes));
            }

            return(null);
        }
Exemple #4
0
        private static WebPNative.MetadataParams CreateWebPMetadata(Document doc, Surface scratchSurface)
        {
            byte[] iccProfileBytes = null;
            byte[] exifBytes       = null;
            byte[] xmpBytes        = null;

            string colorProfile = doc.Metadata.GetUserValue(WebPMetadataNames.ColorProfile);

            if (!string.IsNullOrEmpty(colorProfile))
            {
                iccProfileBytes = Convert.FromBase64String(colorProfile);
            }

            string exif = doc.Metadata.GetUserValue(WebPMetadataNames.EXIF);

            if (!string.IsNullOrEmpty(exif))
            {
                exifBytes = Convert.FromBase64String(exif);
            }

            string xmp = doc.Metadata.GetUserValue(WebPMetadataNames.XMP);

            if (!string.IsNullOrEmpty(xmp))
            {
                xmpBytes = Convert.FromBase64String(xmp);
            }

            if (iccProfileBytes == null || exifBytes == null)
            {
                Dictionary <MetadataKey, MetadataEntry> propertyItems = GetMetadataFromDocument(doc);

                if (propertyItems != null)
                {
                    ExifColorSpace exifColorSpace = ExifColorSpace.Srgb;

                    if (iccProfileBytes != null)
                    {
                        exifColorSpace = ExifColorSpace.Uncalibrated;
                    }
                    else
                    {
                        MetadataKey iccProfileKey = MetadataKeys.Image.InterColorProfile;

                        if (propertyItems.TryGetValue(iccProfileKey, out MetadataEntry iccProfileItem))
                        {
                            iccProfileBytes = iccProfileItem.GetData();
                            propertyItems.Remove(iccProfileKey);
                            exifColorSpace = ExifColorSpace.Uncalibrated;
                        }
                    }

                    if (exifBytes == null)
                    {
                        exifBytes = new ExifWriter(doc, propertyItems, exifColorSpace).CreateExifBlob();
                    }
                }
            }

            if (iccProfileBytes != null || exifBytes != null || xmpBytes != null)
            {
                return(new WebPNative.MetadataParams(iccProfileBytes, exifBytes, xmpBytes));
            }

            return(null);
        }
Exemple #5
0
        private static WebPNative.MetadataParams CreateWebPMetadata(Document doc)
        {
            byte[] iccProfileBytes = null;
            byte[] exifBytes       = null;
            byte[] xmpBytes        = null;

            string colorProfile = doc.Metadata.GetUserValue(WebPMetadataNames.ColorProfile);

            if (!string.IsNullOrEmpty(colorProfile))
            {
                iccProfileBytes = Convert.FromBase64String(colorProfile);
            }

            string exif = doc.Metadata.GetUserValue(WebPMetadataNames.EXIF);

            if (!string.IsNullOrEmpty(exif))
            {
                exifBytes = Convert.FromBase64String(exif);
            }

            string xmp = doc.Metadata.GetUserValue(WebPMetadataNames.XMP);

            if (!string.IsNullOrEmpty(xmp))
            {
                xmpBytes = Convert.FromBase64String(xmp);
            }

            if (iccProfileBytes == null || exifBytes == null)
            {
                Dictionary <ExifPropertyPath, ExifValue> propertyItems = GetMetadataFromDocument(doc);

                if (propertyItems != null)
                {
                    ExifColorSpace exifColorSpace = ExifColorSpace.Srgb;

                    if (propertyItems.TryGetValue(ExifPropertyKeys.Photo.ColorSpace.Path, out ExifValue value))
                    {
                        propertyItems.Remove(ExifPropertyKeys.Photo.ColorSpace.Path);

                        if (MetadataHelpers.TryDecodeShort(value, out ushort colorSpace))
                        {
                            exifColorSpace = (ExifColorSpace)colorSpace;
                        }
                    }

                    if (iccProfileBytes != null)
                    {
                        exifColorSpace = ExifColorSpace.Uncalibrated;
                    }
                    else
                    {
                        ExifPropertyPath iccProfileKey = ExifPropertyKeys.Image.InterColorProfile.Path;

                        if (propertyItems.TryGetValue(iccProfileKey, out ExifValue iccProfileItem))
                        {
                            iccProfileBytes = iccProfileItem.Data.ToArrayEx();
                            propertyItems.Remove(iccProfileKey);
                            exifColorSpace = ExifColorSpace.Uncalibrated;
                        }
                    }

                    if (iccProfileBytes != null)
                    {
                        // Remove the InteroperabilityIndex and related tags, these tags should
                        // not be written if the image has an ICC color profile.
                        propertyItems.Remove(ExifPropertyKeys.Interop.InteroperabilityIndex.Path);
                        propertyItems.Remove(ExifPropertyKeys.Interop.InteroperabilityVersion.Path);
                    }

                    if (exifBytes == null)
                    {
                        exifBytes = new ExifWriter(doc, propertyItems, exifColorSpace).CreateExifBlob();
                    }
                }
            }

            if (xmpBytes == null)
            {
                XmpPacket xmpPacket = doc.Metadata.TryGetXmpPacket();

                if (xmpPacket != null)
                {
                    string xmpPacketAsString = xmpPacket.ToString();

                    xmpBytes = System.Text.Encoding.UTF8.GetBytes(xmpPacketAsString);
                }
            }

            if (iccProfileBytes != null || exifBytes != null || xmpBytes != null)
            {
                return(new WebPNative.MetadataParams(iccProfileBytes, exifBytes, xmpBytes));
            }

            return(null);
        }