StealthRT
StealthRT

Reputation: 10542

ImageMagick Command line to Magick.net C#

Hey all I have the following command line that works great for what I am looking to want to do but unable to translate it over to the C# Magick.net version:

convert YnTf9.png ^
( -clone 0 -blur 0x5 -gravity center -crop 400x300+0+0 +repage ) ^
( -clone 0 -resize x300 ) ^
-delete 0 ^
-gravity center -compose over -composite ^
result.png

So far I have this c# code:

using (MagickImage image = new MagickImage(@"C:\Users\David\Pictures\YnTf9.png"))
{
     using (MagickImage backgroundImg = (MagickImage)image.Clone())
     {
          backgroundImg.Blur(0, 5);
          backgroundImg.Crop(400, 300, Gravity.Center);
          backgroundImg.RePage();
          backgroundImg.Resize(0, 300);

          image.Composite(backgroundImg, Gravity.Center, CompositeOperator.SrcOver);
          image.Write("CODETest.png");
     }
}

After just doing that I ran it and it came out as just the normal image:

enter image description here

When it should look like this:

enter image description here

so I know im not doing ether the correct order and/or correct commands.

Would be great if someone could translate it for me! :)

Upvotes: 0

Views: 3173

Answers (2)

StealthRT
StealthRT

Reputation: 10542

Got it

using (MagickImage image = new MagickImage(bitmap))
    {
        using (IMagickImage backgroundImg = image.Clone())
        {
            backgroundImg.Blur(0, 5);
            backgroundImg.Crop(400, 300, Gravity.Center);
            backgroundImg.RePage();

            image.Resize(0, 300);

            IMagickImage _shadow = new MagickImage(MagickColor.FromRgb(0, 0, 0), image.Width + 20, 400);
_shadow.Shadow(backgroundImg.Width, 400, 10, (Percentage)90);

            backgroundImg.Composite(_shadow, Gravity.Center, CompositeOperator.Atop);
            backgroundImg.Composite(image, Gravity.Center, CompositeOperator.SrcAtop);
            backgroundImg.Write(@"C:\Users\David\Pictures\NEWest.png");
        }
    }

Upvotes: 1

dlemstra
dlemstra

Reputation: 8143

The -clone between the parenthesis creates an individual image:

convert YnTf9.png ^
# Creates a copy of YnTf9.png.
( -clone 0 -blur 0x5 -gravity center -crop 400x300+0+0 +repage ) ^
# Creates another copy of YnTf9.png
( -clone 0 -resize x300 ) ^
# Removes YnTf9.png from the image list, you now only have the two clones
-delete 0 ^
-gravity center -compose over -composite ^
result.png

But in your situation you don't need to create a second clone. Your command can be translated to this:

using (MagickImage image = new MagickImage("i:/YnTf9.png"))
{
    using (IMagickImage backgroundImg = image.Clone())
    {
        backgroundImg.Blur(0, 5);
        backgroundImg.Crop(400, 300, Gravity.Center);
        backgroundImg.RePage();

        // Resize the original image instead of creating a clone, resizing it and then
        // delete the original.
        image.Resize(0, 300);

        backgroundImg.Composite(image, Gravity.Center, CompositeOperator.SrcOver);
        backgroundImg.Write("i:/result.png");
    }
 }

Upvotes: 3

Related Questions