I just reused some code I wrote a couple of years ago, and thought I’d share it here.
It’s not about the best ways to resize images. If that is what you search for, have a look at this 🙂
It’s rather some code for calculating the size of the target image from the originals size while applying one of four rules:
Pretty straight-forward, I think.
var _100_50 = new Size(100, 50);
ResizeType.Fit.CalcNewSize(_100_50, new Size(50, 50), true)
.Satisfy(s => s.Width == 50 && s.Height == 25);
var _50_100 = new Size(50, 100);
ResizeType.Fit.CalcNewSize(_50_100, new Size(50, 50), true)
.Satisfy(s => s.Width == 25 && s.Height == 50);
ResizeType.Stretched.CalcNewSize(_50_100, new Size(500, 500), false)
.Satisfy(s => s.Width == 50 && s.Height == 100);
ResizeType.FixedHeight.CalcNewSize(_50_100, new Size(0, 50), false)
.Satisfy(s => s.Width == 25 && s.Height == 50);
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace lcorneliussen
{
public interface IResizeType
{
Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller);
}
/// <summary>
/// Offers some helpful methods for image resizing.
/// </summary>
public class ResizeType
{
/// <summary>
/// Returns the new size by fixed height.
/// </summary>
public static IResizeType FixedHeight = new FixedHeightResizeType();
/// <summary>
/// Returns the new size by fixed width.
/// </summary>
public static IResizeType FixedWidth = new FixedWidthResizeType();
/// <summary>
/// Returns a size wich fits in a target size.
/// </summary>
public static IResizeType Fit = new FitResizeType();
/// <summary>
/// Stretches a image to the given size.
/// </summary>
public static IResizeType Stretched = new StretchedResizeType();
private static int CalcNewHeight(int currentWidth, int currentHeight, int newWidth)
{
float ratio = (float)newWidth / (float)currentWidth;
float newHeight = currentHeight * ratio;
return (int)newHeight;
}
private static int CalcNewWidth(int currentWidth, int currentHeight, int newHeight)
{
float ratio = (float)newHeight / (float)currentHeight;
float newWidth = currentWidth * ratio;
return (int)newWidth;
}
private class FixedHeightResizeType : IResizeType
{
public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
{
int height = newSize.Height;
if (!resizeIfSmaller)
{
if (height > currentSize.Height) height = currentSize.Height;
}
int newWidth = CalcNewWidth(currentSize.Width, currentSize.Height, height);
return new Size(newWidth, height);
}
}
private class FixedWidthResizeType : IResizeType
{
public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
{
int width = newSize.Width;
if (!resizeIfSmaller)
{
if (width > currentSize.Width) width = currentSize.Width;
}
int newHeight = CalcNewHeight(currentSize.Width, currentSize.Height, width);
return new Size(width, newHeight);
}
}
private class FitResizeType : IResizeType
{
public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
{
int width = newSize.Width;
int height = newSize.Height;
int newWidth = currentSize.Width;
int newHeight = currentSize.Height;
if (!resizeIfSmaller) // break, if newSize allready fits in currentSize
if (currentSize.Height < height && currentSize.Width < width)
return new Size(currentSize.Width, currentSize.Height);
if(width != newWidth) // shrink or expand to width
{
newHeight = CalcNewHeight(currentSize.Width, currentSize.Height, width);
newWidth = width;
}
if (newHeight > height) //shrink to fit height, if neccesarry
{
newWidth = CalcNewWidth(newWidth, newHeight, height);
newHeight = height;
}
return new Size(newWidth, newHeight);
}
}
private class StretchedResizeType : IResizeType
{
public Size CalcNewSize(Size currentSize, Size newSize, bool resizeIfSmaller)
{
int w = newSize.Width;
int h = newSize.Height;
if (!resizeIfSmaller)
{
if (w > currentSize.Width) w = currentSize.Width;
if (h > currentSize.Height) h = currentSize.Height;
}
return new Size(w, h);
}
}
}
}