Created
April 30, 2010 09:55
-
-
Save lcorneliussen/385015 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment