Created
November 11, 2011 13:46
-
-
Save acazsouza/1358038 to your computer and use it in GitHub Desktop.
ImageResize
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
public class ImageResize | |
{ | |
public enum Dimensions | |
{ | |
Width, | |
Height | |
} | |
public enum AnchorPosition | |
{ | |
Top, | |
Center, | |
Bottom, | |
Left, | |
Right | |
} | |
public static Image ScaleByPercent(Image imgPhoto, int Percent) | |
{ | |
float nPercent = (Convert.ToSingle(Percent) / 100); | |
int sourceWidth = imgPhoto.Width; | |
int sourceHeight = imgPhoto.Height; | |
int sourceX = 0; | |
int sourceY = 0; | |
int destX = 0; | |
int destY = 0; | |
int destWidth = Convert.ToInt32((sourceWidth * nPercent)); | |
int destHeight = Convert.ToInt32((sourceHeight * nPercent)); | |
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); | |
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); | |
Graphics grPhoto = Graphics.FromImage(bmPhoto); | |
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); | |
grPhoto.Dispose(); | |
return bmPhoto; | |
} | |
public static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension) | |
{ | |
int sourceWidth = imgPhoto.Width; | |
int sourceHeight = imgPhoto.Height; | |
int sourceX = 0; | |
int sourceY = 0; | |
int destX = 0; | |
int destY = 0; | |
float nPercent = 0; | |
switch (Dimension) | |
{ | |
case Dimensions.Width: | |
nPercent = (Convert.ToSingle(Size) / Convert.ToSingle(sourceWidth)); | |
break; // TODO: might not be correct. Was : Exit Select | |
default: | |
nPercent = (Convert.ToSingle(Size) / Convert.ToSingle(sourceHeight)); | |
break; // TODO: might not be correct. Was : Exit Select | |
} | |
int destWidth = Convert.ToInt32((sourceWidth * nPercent)); | |
int destHeight = Convert.ToInt32((sourceHeight * nPercent)); | |
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); | |
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); | |
Graphics grPhoto = Graphics.FromImage(bmPhoto); | |
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); | |
grPhoto.Dispose(); | |
return bmPhoto; | |
} | |
public static Image FixedSize(Image imgPhoto, int Width, int Height) | |
{ | |
int sourceWidth = imgPhoto.Width; | |
int sourceHeight = imgPhoto.Height; | |
int destX = 0; | |
int destY = 0; | |
float nPercent = 0; | |
float nPercentW = 0; | |
float nPercentH = 0; | |
nPercentW = (Convert.ToSingle(Width) / Convert.ToSingle(sourceWidth)); | |
nPercentH = (Convert.ToSingle(Height) / Convert.ToSingle(sourceHeight)); | |
//if we have to pad the height pad both the top and the bottom | |
//with the difference between the scaled height and the desired height | |
if (nPercentH < nPercentW) | |
{ | |
nPercent = nPercentH; | |
destX = Convert.ToInt32(((Width - (sourceWidth * nPercent)) / 2)); | |
} | |
else | |
{ | |
nPercent = nPercentW; | |
destY = Convert.ToInt32(((Height - (sourceHeight * nPercent)) / 2)); | |
} | |
int destWidth = Convert.ToInt32((sourceWidth * nPercent)); | |
int destHeight = Convert.ToInt32((sourceHeight * nPercent)); | |
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); | |
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); | |
Graphics grPhoto = Graphics.FromImage(bmPhoto); | |
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
grPhoto.DrawImage(imgPhoto, new Rectangle(-1, -1, destWidth + 1, destHeight + 1), new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel); | |
grPhoto.Dispose(); | |
return bmPhoto; | |
} | |
public static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor) | |
{ | |
int sourceWidth = imgPhoto.Width; | |
int sourceHeight = imgPhoto.Height; | |
int sourceX = 0; | |
int sourceY = 0; | |
int destX = 0; | |
int destY = 0; | |
float nPercent = 0; | |
float nPercentW = 0; | |
float nPercentH = 0; | |
nPercentW = (Convert.ToSingle(Width) / Convert.ToSingle(sourceWidth)); | |
nPercentH = (Convert.ToSingle(Height) / Convert.ToSingle(sourceHeight)); | |
if (nPercentH < nPercentW) | |
{ | |
nPercent = nPercentW; | |
switch (Anchor) | |
{ | |
case AnchorPosition.Top: | |
destY = 0; | |
break; // TODO: might not be correct. Was : Exit Select | |
case AnchorPosition.Bottom: | |
destY = Convert.ToInt32((Height - (sourceHeight * nPercent))); | |
break; // TODO: might not be correct. Was : Exit Select | |
default: | |
destY = Convert.ToInt32(((Height - (sourceHeight * nPercent)) / 2)); | |
destX = ((destY == destX) ? 0 : -1); | |
break; // TODO: might not be correct. Was : Exit Select | |
} | |
} | |
else | |
{ | |
nPercent = nPercentH; | |
switch (Anchor) | |
{ | |
case AnchorPosition.Left: | |
destX = 0; | |
break; // TODO: might not be correct. Was : Exit Select | |
case AnchorPosition.Right: | |
destX = Convert.ToInt32((Width - (sourceWidth * nPercent))); | |
break; // TODO: might not be correct. Was : Exit Select | |
default: | |
destX = Convert.ToInt32(((Width - (sourceWidth * nPercent)) / 2)); | |
destY = ((destX == destY) ? 0 : -1); | |
break; // TODO: might not be correct. Was : Exit Select | |
} | |
} | |
int destWidth = Convert.ToInt32((sourceWidth * nPercent)); | |
int destHeight = Convert.ToInt32((sourceHeight * nPercent)); | |
if (destWidth != sourceWidth) | |
{ | |
destHeight += 1; | |
} | |
if (destHeight != sourceHeight) | |
{ | |
destWidth += 1; | |
} | |
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); | |
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); | |
Graphics grPhoto = Graphics.FromImage(bmPhoto); | |
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); | |
grPhoto.Dispose(); | |
return bmPhoto; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment