You almost got it! The name of
SetArtifact
should not contain option:
and you will need to format the expression yourself. This is not done automatically. Below is an example of how you could do this:using (MagickImage image = new MagickImage(@"C:\input_rectangle.jpg")) { // -virtual-pixel background image.VirtualPixelMethod = VirtualPixelMethod.Background; // -background white image.BackgroundColor = MagickColors.White; // -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]"string expression = image.FormatExpression("%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]"); image.SetArtifact("distort:viewport", expression); // An alternate implementation would be:int size = Math.Max(image.Width, image.Height); // [fx:max(w,h)]int x = -Math.Max((image.Height - image.Width) / 2, 0);// -[fx:max((h-w)/2,0)]int y = -Math.Max((image.Width - image.Height) / 2, 0);// -[fx:max((w-h)/2,0)] MagickGeometry geometry = new MagickGeometry(x, y, size, size); image.SetArtifact("distort:viewport", geometry.ToString()); // -filter point image.FilterType = FilterType.Point; // -distort SRT 0 image.Distort(DistortMethod.ScaleRotateTranslate, 0); // params// +repage image.RePage(); image.Write(@"C:\output_square.jpg"); }