views:

211

answers:

2

I want to resize a picture to a new size using one parameter: Width.

If the picture is horizontal, the new size will be: width = Width, height = proportional to width.

And if the picture is vertical, the new size will be: height = Width, width = proportional to height.

Any idea how to implement this?

I'm using ImageMagick with MagickNet wrapper.

+1  A: 

I'm not sure exactly what you mean here. You say you just want to define the width, but in the "vertical" case, you set the height to be the width? Anyway, if you want to resize something using just the width, use this pseudo-code:

ratio = width / height
newWidth = <the new width>
newHeight = newWidth / ratio

If you want to resize the longest size to a given value, try this:

ratio = width / height

if ratio > 1   // wider than it is tall
    newWidth = <theValue>
    newHeight = newWidth / ratio

else           // taller than it is wide
    newHeight = <theValue>
    newWidth = newHeight * ratio
nickf
I assume the 'Width' is the boundary/longest value, so it's your second solution :)
o.k.w
I meant the second one. But I don't have the `width` and `height`. I have the picture path, and the target longest size parameters.
Mendy
+1  A: 

From the usage reference at http://www.imagemagick.org/Usage/resize/

convert org.jpg    -resize widthxwidth  final.jpg

e.g. widthxwidth can be 256x256

The aspect ratio will be kept and the resizing will be done within the boundary of 256 X 256 pixel square.

Quoted from the page above:

Resize will fit the image into the requested size. It does NOT fill, the requested box size.

o.k.w
@o.k.w: exactly what I am looking for. I'm going to test it with MagickNet.
Mendy
Do you have any idea how can I implement it in .net?
Mendy
@Menday, I would recommend you ask a new question on ImageMagick.NET :)
o.k.w