示例#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>
    /// 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);
            }
    }
    /// <summary>
    /// Modifies clipping path explicitly
    /// </summary>
    private static void ModifyClippingPathExplicitly()
    {
        using (var reader = new JpegReader("../../../../_Input/Apple.jpg"))
            using (var bitmap = reader.Frames[0].GetBitmap())
            {
                var crop = new Crop(20, 20, bitmap.Width - 40, bitmap.Height - 40);

                var cropped = crop.Apply(bitmap);

                var clippingPath = reader.ClippingPaths[0];

                clippingPath.ApplyTransform(crop.GetPathTransformMatrix(bitmap.Width, bitmap.Height).ToGdiPlusMatrix());

                var adobeResources = new AdobeResourceDictionary();
                adobeResources.Add(FirstPathId, new AdobeResourceBlock("Apple", clippingPath.Data));

                var jpegSettings = new JpegSettings();
                jpegSettings.AdobeResources = adobeResources;

                cropped.Save("../../../../_Output/ModifyClippingPathExplicitly.jpg", jpegSettings);
            }
    }
    /// <summary>
    /// Copies clipping path using memory-friendly Pipeline API
    /// </summary>
    private static void CopyClippingPathMemoryFriendly()
    {
        using (var reader = new JpegReader("../../../../_Input/Apple.jpg"))
            using (var writer = new JpegWriter("../../../../_Output/CopyClippingPathMemoryFriendly.jpg"))
            {
                if (reader.AdobeResources != null)
                {
                    var adobeResources = new AdobeResourceDictionary();

                    for (int i = FirstPathId; i <= LastPathId; i++)
                    {
                        if (reader.AdobeResources.Contains(i))
                        {
                            adobeResources[i] = reader.AdobeResources[i];
                        }
                    }

                    writer.AdobeResources = adobeResources;
                }

                Pipeline.Run(reader + writer);
            }
    }