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

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

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

Post 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);
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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 ;)
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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?
snibgo's IM pages: im.snibgo.com
GoncaloM
Posts: 18
Joined: 2019-05-23T08:20:39-07:00
Authentication code: 1152

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Landscape is W > H. So I am also confused about what you need.
Post Reply