c# crop image to a Thumbnail
By 4usky
Demand from an album function, typically users to upload photos later, we are against the photo and then generates a thumbnail for a list of other pages on the display.
OK, now to discuss this thumbnail generation. Casual look at the major sites are basically the Original geometric scaling to generate the thumbnail. But the perfectionist will find some of the problems, such as the display layout under the photo albums when you want the thumbnail list of great unity, neat, and beautiful, such as the requirement for each thumbnail size is fixed at 120 x 90 and can not be stretched variant of how to do? Users to upload photos varied, horizontal, vertical, and square ......, how to do the same for each size of the thumbnails and not stretched variants do?
Go to the subject: the code inside than the GOOGLE Search for "C # image cropping," Most of the results must be advanced so a little bit, the code also contains a C # image cutting some basic knowledge and want to get into a friend help.
I thought the logic of the first thumbnail depending on the target ratio, in order to image center as a cutting center, to the Original to the widest range of cutting, then cutting the results of geometric scaling, the following sample code for the local processing results
#region crop image
/// <summary>
/// upload image
/// crop
/// </summary>
/// <param name="postedFile">HttpPostedFile</param>
/// <param name="templateWidth">width(unit:px)</param>
/// <param name="templateHeight">height(unit:px)</param>
/// <param name="fileSaveUrl">save url</param>
public static void UploadImage(System.Web.HttpPostedFile postedFile, int templateWidth, int templateHeight, string fileSaveUrl)
{
System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);
if (initImage.Width <= templateWidth && initImage.Height <= templateHeight)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
double templateRate = double.Parse(templateWidth.ToString()) / templateHeight;
double initRate = double.Parse(initImage.Width.ToString()) / initImage.Height;
if (templateRate == initRate)
{
System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG = null;
Rectangle fromR = new Rectangle(0, 0, 0, 0);
Rectangle toR = new Rectangle(0, 0, 0, 0);
if (templateRate > initRate)
{
pickedImage = new System.Drawing.Bitmap(initImage.Width, int.Parse(Math.Floor(initImage.Width / templateRate).ToString()));
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
fromR.X = 0;
fromR.Y = int.Parse(Math.Floor((initImage.Height - initImage.Width / templateRate) / 2).ToString());
fromR.Width = initImage.Width;
fromR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
toR.X = 0;
toR.Y = 0;
toR.Width = initImage.Width;
toR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
}
else
{
pickedImage = new System.Drawing.Bitmap(int.Parse(Math.Floor(initImage.Height * templateRate).ToString()), initImage.Height);
pickedG = System.Drawing.Graphics.FromImage(pickedImage);
fromR.X = int.Parse(Math.Floor((initImage.Width - initImage.Height * templateRate) / 2).ToString());
fromR.Y = 0;
fromR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
fromR.Height = initImage.Height;
toR.X = 0;
toR.Y = 0;
toR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
toR.Height = initImage.Height;
}
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
System.Drawing.Graphics templateG = System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(pickedImage, new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);
templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
templateG.Dispose();
templateImage.Dispose();
pickedG.Dispose();
pickedImage.Dispose();
}
}
initImage.Dispose();
}
#endregion
Brandon E Newman 2 years ago
Did you use some sort of software to translate your text into English?