How do you resize the width of a image in C# without resizing the height using image.resize()
When I do it this way:
image.Resize(width: 800, preserveAspectRatio: true,preventEnlarge:true);
This is the full code:
var imagePath = "";
var newFileName = "";
var imageThumbPath = "";
WebImage image = null;
image = WebImage.GetImageFromRequest();
if (image != null)
{
newFileName = Path.GetFileName(image.FileName);
imagePath = @"pages/"+newFileName;
image.Resize(width:800, preserveAspectRatio:true, preventEnlarge:true);
image.Save(@"~/images/" + imagePath);
imageThumbPath = @"pages/thumbnail/"+newFileName;
image.Resize(width: 150, height:150, preserveAspectRatio:true, preventEnlarge:true);
image.Save(@"~/images/" + imageThumbPath);
}
I get this error message:
No overload for method ‘Resize’ takes 3 arguments
The documentation is garbage, so I peeked at the source code. The logic they are using is to look at the values passed for height and width and compute aspect ratios for each comparing the new value to the current value. Whichever value (height or width) has the greater aspect ratio gets its value computed from the other value. Here’s the relevant snippet:
So, what that means is that, if you don’t want to compute the height value yourself, just pass in a height value that is very large.
This will cause
hRatioto be larger thanwRatioand thenheightwill be computed based onwidth.Since you have
preventEnlargeset totrue, you could just passimage.Heightin.Of course, it’s not difficult to just compute
heightyourself: