Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
Created January 12, 2016 22:42
Show Gist options
  • Save LGM-AdrianHum/8bf55b7ed80dc39ef74c to your computer and use it in GitHub Desktop.
Save LGM-AdrianHum/8bf55b7ed80dc39ef74c to your computer and use it in GitHub Desktop.
Image Management Code (Cropping And Resizing) In C#
public static Image ResizeImage(Image imgToResize, Size destinationSize)
{
var originalWidth = imgToResize.Width;
var originalHeight = imgToResize.Height;
//how many units are there to make the original length
var hRatio = (float)originalHeight/destinationSize.Height;
var wRatio = (float)originalWidth/destinationSize.Width;
//get the shorter side
var ratio = Math.Min(hRatio, wRatio);
var hScale = Convert.ToInt32(destinationSize.Height * ratio);
var wScale = Convert.ToInt32(destinationSize.Width * ratio);
//start cropping from the center
var startX = (originalWidth - wScale)/2;
var startY = (originalHeight - hScale)/2;
//crop the image from the specified location and size
var sourceRectangle = new Rectangle(startX, startY, wScale, hScale);
//the future size of the image
var bitmap = new Bitmap(destinationSize.Width, destinationSize.Height);
//fill-in the whole bitmap
var destinationRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
//generate the new image
using (var g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return bitmap;
}
// var thumbImage = ImageHelper.ResizeImage(image, new Size(45, 45));
// thumbImage.Save(thumbFullPath);
bool SaveCroppedImage(Image image, int targetWidth, int targetHeight, string filePath)
{
ImageCodecInfo jpgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == "image/jpeg").First();
Image finalImage = image;
System.Drawing.Bitmap bitmap = null;
try
{
int left = 0;
int top = 0;
int srcWidth = targetWidth;
int srcHeight = targetHeight;
bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
double croppedHeightToWidth = (double)targetHeight / targetWidth;
double croppedWidthToHeight = (double)targetWidth / targetHeight;
if (image.Width > image.Height)
{
srcWidth = (int)(Math.Round(image.Height * croppedWidthToHeight));
if (srcWidth < image.Width)
{
srcHeight = image.Height;
left = (image.Width - srcWidth) / 2;
}
else
{
srcHeight = (int)Math.Round(image.Height * ((double)image.Width / srcWidth));
srcWidth = image.Width;
top = (image.Height - srcHeight) / 2;
}
}
else
{
srcHeight = (int)(Math.Round(image.Width * croppedHeightToWidth));
if (srcHeight < image.Height)
{
srcWidth = image.Width;
top = (image.Height - srcHeight) / 2;
}
else
{
srcWidth = (int)Math.Round(image.Width * ((double)image.Height / srcHeight));
srcHeight = image.Height;
left = (image.Width - srcWidth) / 2;
}
}
using (Graphics g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(left, top, srcWidth, srcHeight), GraphicsUnit.Pixel);
}
finalImage = bitmap;
}
catch { }
try
{
using (EncoderParameters encParams = new EncoderParameters(1))
{
encParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);
//quality should be in the range [0..100] .. 100 for max, 0 for min (0 best compression)
finalImage.Save(filePath, jpgInfo, encParams);
return true;
}
}
catch { }
if (bitmap != null)
{
bitmap.Dispose();
}
return false;
}
int originalHeight;
int originalWidth;
int imageHeight;
int imageWidth;
int requiredHeight;
int requiredWidth;
double scale;
if(originalHeight > requiredHeight)
{
scale = requiredHeight / originalHeight;
imageHeight = requiredHeight;
imageWidth = originalHeight * scale;
}
if(imageWidth > requiredWidth)
{
scale = requiredWidth / imageWidth;
imageWidth = requiredWidth;
imageHeight = imageHeight * scale;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment