patterncsharpMinor
Internal function that determines when to resize by width or height in a Image Resizer utility
Viewed 0 times
imageheightresizefunctionresizerutilitydetermineswidththatwhen
Problem
Description:
Given an image X with known dimensions. We call a method
If no max-width/max-height, the width and height should return as is.
If only one is provided, then we call a method ResizeWidth on the Dimention2D object to return a new dimension based on the new width size (aspectRation is kept).
Same applies for height.
If both are provided, use the one, that while keeping the aspect ration, won't be bigger than their corresponding maximum.
If the image dimension are smaller than the maximum, no change to the dimensions.
Then, with the new size, the Resize is actually applied (or not) (this is outside the scope of this method).
I'm open to any suggestion.
Variables/methods names, the use (or lack of)
Given an image X with known dimensions. We call a method
CreateThumbnail to make a thumbnail (or not) with a given max-width and max-height (both optional).CreateThumbnail call ComputeSize method (bad name?) which is responsable for doing the following logic:If no max-width/max-height, the width and height should return as is.
If only one is provided, then we call a method ResizeWidth on the Dimention2D object to return a new dimension based on the new width size (aspectRation is kept).
Same applies for height.
If both are provided, use the one, that while keeping the aspect ration, won't be bigger than their corresponding maximum.
If the image dimension are smaller than the maximum, no change to the dimensions.
Then, with the new size, the Resize is actually applied (or not) (this is outside the scope of this method).
private Dimention2D ComputeSize(Image image, int maxWidth, int maxHeight)
{
if (image == null) throw new ArgumentNullException(nameof(image));
var sourceDimension = new Dimention2D(image.Width, image.Height);
bool computeWidth = maxWidth > 0 && sourceDimension.Width > maxWidth;
bool computeHeight = maxHeight > 0 && sourceDimension.Height > maxHeight;
if (computeWidth && computeHeight)
{
if (sourceDimension.Width / maxWidth > sourceDimension.Height / maxHeight)
computeHeight = false;
else
computeWidth = false;
}
if (computeWidth)
return sourceDimension.ResizeWidth(maxWidth);
else if (computeHeight)
return sourceDimension.ResizeHeight(maxHeight);
else return sourceDimension;
}I'm open to any suggestion.
Variables/methods names, the use (or lack of)
{ and } on if/else, the if/else. and whatever thing you can think of making it prettier.Solution
You can replace this:
with this:
I would rename the method and input parameters, as it would add more sense to the method name and input parameters.
if (computeWidth)
return sourceDimension.ResizeWidth(maxWidth);
else if (computeHeight)
return sourceDimension.ResizeHeight(maxHeight);
else return sourceDimension;with this:
if (computeWidth)
return sourceDimension.ResizeWidth(maxWidth);
if (computeHeight)
return sourceDimension.ResizeHeight(maxHeight);
return sourceDimension;I would rename the method and input parameters, as it would add more sense to the method name and input parameters.
private Dimention2D ReSize(Image image, int requireWidth, int requireHeight);Code Snippets
if (computeWidth)
return sourceDimension.ResizeWidth(maxWidth);
else if (computeHeight)
return sourceDimension.ResizeHeight(maxHeight);
else return sourceDimension;if (computeWidth)
return sourceDimension.ResizeWidth(maxWidth);
if (computeHeight)
return sourceDimension.ResizeHeight(maxHeight);
return sourceDimension;private Dimention2D ReSize(Image image, int requireWidth, int requireHeight);Context
StackExchange Code Review Q#126277, answer score: 3
Revisions (0)
No revisions yet.