coinsgift.blogg.se

Image resize aspect ratio calculator
Image resize aspect ratio calculator












The end result is that a lot of variables that were using are now gone e.g.

Image resize aspect ratio calculator code#

I've written a console app that tries to illustrate how you could simplify your code a bit. If (newWidth > BOXWIDTH || newHeight > BOXWIDTH) if one of the two dimensions exceed the box dimensions calculate new dimensions based on aspect ratio Private static ImageDimensions CalculateNewDimensionsForImage(Bitmap original)įloat aspect = original.Width / (float)original.Height Private static bool ImageIsBox(Bitmap original) Using (FileStream fs = new FileStream(imagePath, FileMode.Open)) Private static Bitmap LoadOriginalImage(string imagePath) Return new Bitmap(original, newDimensions.Width, newDimensions.Height)

image resize aspect ratio calculator

Var newDimensions = CalculateNewDimensionsForImage(original) Return new Bitmap(original, BOXWIDTH, BOXWIDTH) Here's how you can refactor your code a bit: namespace ConsoleApplication1īitmap original = LoadOriginalImage(imageLabel.Text) ResizedImage = new Bitmap(original, newWidth, newHeight) depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio In the current example rectHeight rectWidth || newHeight > rectHeight)

image resize aspect ratio calculator

if the image is squared set it's height and width to the smallest of the desired dimensions (our box). Using (FileStream fs = new System.IO.FileStream(imageLabel.Text, System.IO.FileMode.Open)) calculations and determining which side is longer, followed by sh and sw which uses the step to adjust the pixels, some aspect ratio equations. Is there a more efficient way to do this? Is there something wrong with my code (from the results it looks ok)? Is there a "shorter" version to do this? Bitmap original, resizedImage to calculate the image resizing you can use the following calculation: Max width: 1920 Max height: 1280. I think that some parts of the code are not needed (for example if it's a box image).

image resize aspect ratio calculator

I wrote some code that resizes an image to fit in a given box.












Image resize aspect ratio calculator