Page 1 of 1

How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-26T14:54:00-07:00
by GoncaloM
Hi everyone.

I'm trying to achieve some transformations on images using Magick.NET and I got some useful help by getting the right command-line:

Code: Select all

magick C:\Users\gm\Desktop\Vader.jpg -resize 1280x1920^^ -gravity center -crop 1280x1920+0+0 +repage C:\Users\gm\Desktop\Vader.jpg


The problem is that I'm not being able to map it and get the same result using Magick.NET.
The code I currently have is the following (the original image size is 2732 x 2732 and the resulting image form my code has 1280x1280 rather than 1280x1920):

Code: Select all

using (MagickImage image = new MagickImage(ssimageBinary))
{
	image.Density = new Density(sstargetDensity);
	MagickGeometry size = new MagickGeometry(sstargetWidth, sstargetHeight);

	image.Resize(size);
	image.Crop(sstargetWidth, sstargetHeight, Gravity.Center);
	image.RePage();

	
	// Save the result and return it has binary data
	MemoryStream mStream = new MemoryStream();
	image.Write(mStream, MagickFormat.Png);
	ssnewImage = mStream.ToArray();
}
Hope someone can help me with this.
Cheers

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-26T22:44:00-07:00
by dlemstra
You will need to change:

Code: Select all

new MagickGeometry(sstargetWidth, sstargetHeight)
into

Code: Select all

new MagickGeometry($"{sstargetWidth}x{sstargetHeight}^")
or add

Code: Select all

size.FillArea=true;
And you could also use

Code: Select all

ssnewImage = image.ToByteArray(MagickFormat.Png);

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-27T00:42:25-07:00
by GoncaloM
dlemstra wrote: 2019-05-26T22:44:00-07:00 You will need to change:

Code: Select all

new MagickGeometry(sstargetWidth, sstargetHeight)
into

Code: Select all

new MagickGeometry($"{sstargetWidth}x{sstargetHeight}^")
or add

Code: Select all

size.FillArea=true;
And you could also use

Code: Select all

ssnewImage = image.ToByteArray(MagickFormat.Png);
Hi dlemstra.
That worked perfectly, thank you very much.
Also, thank you for the tip to convert to byte array.
Cheers ;)

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-29T00:45:49-07:00
by GoncaloM
Hello.
I'm using the code above but now I'm having an issue on the procedure to take, meaning, according to the initial image size (square or rectangle width > height or height > width) and, according to the final transformation size width > height or height > width I'm getting some images cut, for example when I have the initial image I being a square and it works fine if height > width but doesn't give me the result I want if width > height .
What should be the correct procedure to adapt the code according to all these scenarios?

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-29T06:34:51-07:00
by snibgo
Please don't ask the same question in multiple threads. I have deleted your other post.

I don't understand the question. I suppose you need to write some code to correctly process landscape or portrait inputs.

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-29T08:38:27-07:00
by GoncaloM
Hi.
So I have the following code:

Code: Select all

                using (MagickImage image = new MagickImage(ssimageBinary))
                {
                    //Define density if set in the input
                    if(sstargetDensity > 0)
                        image.Density = new Density(sstargetDensity);

                    MagickGeometry size = new MagickGeometry(sstargetWidth, sstargetHeight);

                   size.FillArea = true;
                   image.Resize(size);
                   image.Crop(sstargetWidth, sstargetHeight, Gravity.Center);
                   image.RePage();
                   ssnewImage = image.ToByteArray(MagickFormat.Png);
                }
I have an image as input with 2732 x 2732 (https://www.screencast.com/t/SSzcxn28V) and:
1- Output set with 960x1440 (width < height) - The result is just fine: https://www.screencast.com/t/5qFgCbGHn3
2- Output set with 1920x1280 (width > height) - The result is not what I wanted: https://www.screencast.com/t/x4oakx7hWdlI

Cheers

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-29T15:21:51-07:00
by GoncaloM
GoncaloM wrote: 2019-05-29T08:38:27-07:00 Hi.
So I have the following code:

Code: Select all

                using (MagickImage image = new MagickImage(ssimageBinary))
                {
                    //Define density if set in the input
                    if(sstargetDensity > 0)
                        image.Density = new Density(sstargetDensity);

                    MagickGeometry size = new MagickGeometry(sstargetWidth, sstargetHeight);

                   size.FillArea = true;
                   image.Resize(size);
                   image.Crop(sstargetWidth, sstargetHeight, Gravity.Center);
                   image.RePage();
                   ssnewImage = image.ToByteArray(MagickFormat.Png);
                }
I have an image as input with 2732 x 2732 (https://www.screencast.com/t/SSzcxn28V) and:
1- Output set with 960x1440 (width < height) - The result is just fine: https://www.screencast.com/t/5qFgCbGHn3
2- Output set with 1920x1280 (width > height) - The result is not what I wanted: https://www.screencast.com/t/x4oakx7hWdlI

Cheers
Hi snibgo,
Did you got my issue now?
Do you think you can help me on this?

Cheers

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-29T15:56:12-07:00
by snibgo
GoncaloM wrote:2- Output set with 1920x1280 (width > height) - The result is not what I wanted: ...
But what is wrong with it? What do you want instead?

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-30T06:18:54-07:00
by GoncaloM
snibgo wrote: 2019-05-29T15:56:12-07:00
GoncaloM wrote:2- Output set with 1920x1280 (width > height) - The result is not what I wanted: ...
But what is wrong with it? What do you want instead?
Basically what I need to do is something like a mobile splash screen image generator.
Do you know how it usually works best for the transformations for landscape? Maybe I'm looking the right way to the problem.

Re: How to get an equivalent Magick.NET script from a command-line command?

Posted: 2019-05-30T11:35:45-07:00
by fmw42
Landscape is W > H. So I am also confused about what you need.