Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Squaring an image

$
0
0
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");
}

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>