コード例 #1
0
    private static void ResizeAndPreserveMetadata()
    {
        using (var jpegReader = new JpegReader("../../../../_Input/Chicago.jpg"))
            using (var resizer = new Resize(jpegReader.Width / 2, 0))
                using (var jpegWriter = new JpegWriter("../../../../_Output/ResizeAndPreserveMetadata.jpg"))
                {
                    // Read EXIF
                    var exif = jpegReader.Exif;
                    // Check if loaded image contains EXIF metadata
                    if (exif == null)
                    {
                        exif = new ExifDictionary();
                    }
                    exif[ExifDictionary.Software] = "Aurigma Graphics Mill";

                    // Read IPTC
                    var iptc = jpegReader.Iptc;
                    // Check if loaded image contains IPTC metadata
                    if (iptc == null)
                    {
                        iptc = new IptcDictionary();
                    }
                    iptc[IptcDictionary.Keyword] = "mountain";

                    // Read Adobe resource blocks
                    var adobeResources = jpegReader.AdobeResources;
                    // Check if loaded image contains Adobe image resource blocks
                    if (adobeResources == null)
                    {
                        adobeResources = new AdobeResourceDictionary();
                    }
                    // Create new adobe image resource block containing copyright metadata
                    var arBlock = new AdobeResourceBlock("Copyright", new byte[] { 1 });
                    // Set this block to the item with 0x040A ID (copyright flag)
                    adobeResources[0x040A] = arBlock;

                    // Read XMP
                    var xmp = new XmpData();
                    //Check if loaded image contains XMP metadata
                    if (jpegReader.Xmp != null)
                    {
                        xmp.Load(jpegReader.Xmp);
                    }
                    // Create a node containing dc:contributor metadata
                    var node = new XmpValueNode(XmpNodeType.SimpleProperty, "John Doe", XmpTagNames.DCContributor);
                    xmp.AddNode(node);

                    // Write metadata
                    jpegWriter.Exif           = exif;
                    jpegWriter.Iptc           = iptc;
                    jpegWriter.AdobeResources = adobeResources;
                    jpegWriter.Xmp            = xmp.Save();

                    Pipeline.Run(jpegReader + resizer + jpegWriter);
                }
    }
コード例 #2
0
        /// <summary>
        /// Updates metdata of JPEG image without recompression
        /// </summary>
        private static void UpdateJpegMetadataLosslessly()
        {
            using (var losslessJpeg = new LosslessJpeg("../../../../_Input/Chicago.jpg"))
            {
                // IPTC
                if (losslessJpeg.Iptc == null)
                {
                    losslessJpeg.Iptc = new IptcDictionary();
                }

                losslessJpeg.Iptc[IptcDictionary.Caption] = "Mountain";

                // EXIF
                if (losslessJpeg.Exif == null)
                {
                    losslessJpeg.Exif = new ExifDictionary();
                }

                losslessJpeg.Exif[ExifDictionary.Software] = "Aurigma Graphics Mill";

                // XMP
                var xmp = new XmpData();
                if (losslessJpeg.Xmp != null)
                {
                    xmp.Load(losslessJpeg.Xmp);
                }

                var node = new XmpValueNode(XmpNodeType.SimpleProperty, "John Doe", XmpTagNames.DCContributor);
                xmp.AddNode(node);
                losslessJpeg.Xmp = xmp.Save();

                // Adobe image resource blocks
                if (losslessJpeg.AdobeResources == null)
                {
                    losslessJpeg.AdobeResources = new AdobeResourceDictionary();
                }

                const int copyrightFlag = 0x040A;

                var arBlock = new AdobeResourceBlock("Copyright", new byte[] { 1 });
                losslessJpeg.AdobeResources[copyrightFlag] = arBlock;

                losslessJpeg.Write("../../../../_Output/UpdateJpegMetadataLosslessly.jpg");
            }
        }
コード例 #3
0
    /// <summary>
    /// Write copyright XMP block
    /// </summary>
    private static void WriteAdobeResourceBlock(string inputPath, string outputPath)
    {
        using (var reader = new JpegReader(inputPath))
            using (var writer = new JpegWriter(outputPath))
            {
                var adobeResources = reader.AdobeResources;
                if (adobeResources == null)
                {
                    adobeResources = new AdobeResourceDictionary();
                }

                // Create new adobe image resource block with the required metadata
                var arBlock = new AdobeResourceBlock("Copyright", new byte[] { 1 });
                // Set this block to the item with 0x040A ID (copyright flag)
                adobeResources[0x040A] = arBlock;
                // Remove a block with 0x0409 (thumbnail data)
                adobeResources.Remove(0x0409);

                writer.AdobeResources = adobeResources;
                Pipeline.Run(reader + writer);
            }
    }