public static XmpRecipeContainer ParseXmlRecepie(XDocument xdocument) { //Watermarks, Frames and Text are not supported //Those are stored in DFM Streams which are very complicated to read: //https://www.mitec.cz/dfm.html //https://github.com/andriy-gerasika/dfm2xml/blob/master/dfm2xmlImpl.pas //https://github.com/jean-lopes/dfm-to-json //https://github.com/jackdp/DfmExtractor //Maybe it would be possible to implement the Wartermarks, b //but the Frame and Text element are also rendered dynamically and this would be very complicated. XmpRecipeContainer xmpRecipeContainer = new XmpRecipeContainer(); IEnumerable <XNode> recipeNodes = null; if (xdocument != null) { var xmpQuery = from descriptions in xdocument.Descendants(xNameDescription) where descriptions.Attribute(xNameIles) != null select descriptions; recipeNodes = xmpQuery.First().Nodes(); } if (recipeNodes != null) { foreach (XElement element in recipeNodes.OfType <XElement>()) { XElement currentRecipe = element.Descendants().First(); if (element.Name.Equals(xNameIlesRotate)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesRotate_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesRotate_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesRotate_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesRotate_BlendMode); XElement angleElement = getXElementByXName(currentRecipe, xNameIlesRotate_Angle); XElement cropElement = getXElementByXName(currentRecipe, xNameIlesRotate_Crop); XElement backgroundColorElement = getXElementByXName(currentRecipe, xNameIlesRotate_BackgroundColor); if (recipeEnabledElement.Value.Equals("1") & angleElement != null) { double angle = Double.Parse(angleElement.Value); XmpRotate xmpRotate = new XmpRotate { Angle = angle }; xmpRecipeContainer.Actions.Add(xmpRotate); } } else if (element.Name.Equals(xNameIlesResizeFixed)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_FriendlyName); XElement guidElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_GUID); XElement resizeFixedEnabledElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Enabled); XElement widthElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Width); XElement heightElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Height); XElement widthDisplayUnitElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_WidthDisplayUnit); XElement heightDisplayUnitElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_HeightDisplayUnit); XElement adjustResolutionElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_AdjustResolution); XElement resolutionElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Resolution); XElement resolutionDisplayUnitElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_ResolutionDisplayUnit); XElement proportionalElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Proportional); XElement proportionalSettingElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_ProportionalSetting); XElement enlargeSmallImagesElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_EnlargeSmallImages); XElement maintainRatioElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_MaintainRatio); XElement interpolatedElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Interpolated); XElement filterElement = getXElementByXName(currentRecipe, xNameIlesResizeFixed_Filter); if (recipeEnabledElement.Value.Equals("1")) { int intWidth = Int32.Parse(widthElement.Value); int intHeight = Int32.Parse(heightElement.Value); XmpResize xmpResize = new XmpResize { Width = intWidth, Height = intHeight }; xmpRecipeContainer.Actions.Add(xmpResize); } } else if (element.Name.Equals(xNameIlesStraighten)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesStraighten_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesStraighten_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesStraighten_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesStraighten_BlendMode); XElement angleElement = getXElementByXName(currentRecipe, xNameIlesStraighten_Angle); XElement cropElement = getXElementByXName(currentRecipe, xNameIlesStraighten_Crop); XElement BackgroundColorElement = getXElementByXName(currentRecipe, xNameIlesStraighten_BackgroundColor); if (recipeEnabledElement.Value.Equals("1") & angleElement != null) { double angle = Double.Parse(angleElement.Value); bool crop = cropElement != null && cropElement.Value.Equals("1") ? true : false; XmpStraighten xmpStraighten = new XmpStraighten { Angle = angle, Crop = crop }; xmpRecipeContainer.Actions.Add(xmpStraighten); } } else if (element.Name.Equals(xNameIlesFlip)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesFlip_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesFlip_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesFlip_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesFlip_BlendMode); XElement flipVerticalElement = getXElementByXName(currentRecipe, xNameIlesFlip_FlipVertical); XElement flipHorizontalElement = getXElementByXName(currentRecipe, xNameIlesFlip_FlipHorizontal); if (recipeEnabledElement.Value.Equals("1")) { XmpFlip xmpFlip = new XmpFlip { FlipVertical = flipVerticalElement != null && flipVerticalElement.Value.Equals("1") ? true : false, FlipHorizontal = flipHorizontalElement != null && flipHorizontalElement.Value.Equals("1") ? true : false, }; xmpRecipeContainer.Actions.Add(xmpFlip); } } else if (element.Name.Equals(xNameIlesFrame)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesFrame_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesFrame_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesFrame_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesFrame_BlendMode); XElement frameStreamElement = getXElementByXName(currentRecipe, xNameIlesFrame_FrameStream); //if (recipeEnabledElement.Value.Equals("1")) //{ // XmpWatermark xmpWatermark = new XmpWatermark // { // Watermark = Convert.FromBase64String(frameStreamElement.Value) // }; // xmpRecipeContainer.Actions.Add(xmpWatermark); // Hierbei handelt es sich um einen Delphi resource Stream. // //File.WriteAllBytes("d:\\Frame.png", xmpWatermark.Watermark); //} } else if (element.Name.Equals(xNameIlesTitle)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesTitle_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesTitle_FriendlyName); XElement titleStreamElement = getXElementByXName(currentRecipe, xNameIlesTitle_TitleStream); //if (recipeEnabledElement.Value.Equals("1")) //{ // XmpWatermark xmpWatermark = new XmpWatermark // { // Watermark = Convert.FromBase64String(titleStreamElement.Value) // }; // xmpRecipeContainer.Actions.Add(xmpWatermark); // Hierbei handelt es sich um einen Delphi resource Stream. // //File.WriteAllBytes("d:\\Title.png", xmpWatermark.Watermark); //} } else if (element.Name.Equals(xNameIlesWatermarks)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesWatermarks_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesWatermarks_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesWatermarks_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesWatermarks_BlendMode); XElement watermarksStreamElement = getXElementByXName(currentRecipe, xNameIlesWatermarks_WatermarksStream); //if (recipeEnabledElement.Value.Equals("1")) //{ // XmpWatermark xmpWatermark = new XmpWatermark // { // Watermark = Convert.FromBase64String(watermarksStreamElement.Value) // }; // xmpRecipeContainer.Actions.Add(xmpWatermark); // Hierbei handelt es sich um einen Delphi resource Stream. // //File.WriteAllBytes("d:\\Watermark.png", xmpWatermark.Watermark); //} } else if (element.Name.Equals(xNameIlesCrop)) { XElement recipeEnabledElement = getXElementByXName(currentRecipe, xNameIlesCrop_RecipeEnabled); XElement friendlyNameElement = getXElementByXName(currentRecipe, xNameIlesCrop_FriendlyName); XElement opacityElement = getXElementByXName(currentRecipe, xNameIlesCrop_Opacity); XElement blendModeElement = getXElementByXName(currentRecipe, xNameIlesCrop_BlendMode); XElement leftElement = getXElementByXName(currentRecipe, xNameIlesCrop_Left); XElement topElement = getXElementByXName(currentRecipe, xNameIlesCrop_Top); XElement rightElement = getXElementByXName(currentRecipe, xNameIlesCrop_Right); XElement bottomElement = getXElementByXName(currentRecipe, xNameIlesCrop_Bottom); if (recipeEnabledElement.Value.Equals("1")) { XmpCrop xmpCrop = new XmpCrop() { Left = double.Parse(leftElement.Value), Top = double.Parse(topElement.Value), Right = double.Parse(rightElement.Value), Bottom = double.Parse(bottomElement.Value) }; xmpRecipeContainer.Actions.Add(xmpCrop); } } } } return(xmpRecipeContainer); }
public static void ApplyXmpRecipe(XmpRecipeContainer xmpRecipeContainer, MagickImage image) { foreach (IXmpRecipeAction action in xmpRecipeContainer.Actions) { if (action.GetType() == typeof(XmpRotate)) { XmpRotate xmpRotate = (XmpRotate)action; image.VirtualPixelMethod = VirtualPixelMethod.Black; image.Rotate(xmpRotate.Angle); } else if (action.GetType() == typeof(XmpResize)) { XmpResize xmpResize = (XmpResize)action; if (image.Width > xmpResize.Width && image.Height > xmpResize.Height) { image.Resize(xmpResize.Width, xmpResize.Height); } } else if (action.GetType() == typeof(XmpCrop)) { XmpCrop xmpCrop = (XmpCrop)action; int intLeftPixel = Convert.ToInt32(image.Width * xmpCrop.Left); int intTopPixel = Convert.ToInt32(image.Height * xmpCrop.Top); int intRightPixel = Convert.ToInt32(image.Width * xmpCrop.Right); int intBottomPixel = Convert.ToInt32(image.Height * xmpCrop.Bottom); int intWidth = intRightPixel - intLeftPixel; int intHeight = intBottomPixel - intTopPixel; MagickGeometry magickGeometry = new MagickGeometry(intLeftPixel, intTopPixel, intWidth, intHeight); image.Crop(magickGeometry); } else if (action.GetType() == typeof(XmpFlip)) { XmpFlip xmpFlip = (XmpFlip)action; if (xmpFlip.FlipVertical) { image.Flop(); } if (xmpFlip.FlipHorizontal) { image.Flip(); } } else if (action.GetType() == typeof(XmpStraighten)) { XmpStraighten xmpStraighten = (XmpStraighten)action; image.VirtualPixelMethod = VirtualPixelMethod.Black; if (xmpStraighten.Crop) { //http://www.imagemagick.org/Usage/distorts/#rotate_methods int w = image.Width; int h = image.Height; double aa = xmpStraighten.Angle * Math.PI / 180; double srt = (w * Math.Abs(Math.Sin(aa)) + h * Math.Abs(Math.Cos(aa))) / Math.Min(w, h); image.Distort(DistortMethod.ScaleRotateTranslate, srt, xmpStraighten.Angle); } else { image.Rotate(xmpStraighten.Angle); } } } }