Skip to content

Instantly share code, notes, and snippets.

@acazsouza
Created November 11, 2011 13:46
Show Gist options
  • Save acazsouza/1358038 to your computer and use it in GitHub Desktop.
Save acazsouza/1358038 to your computer and use it in GitHub Desktop.
ImageResize
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