更新时间:2023-01-04 12:28:38
public void ResizeImage(string origFileLocation, string newFileLocation, string newFileName, int newWidth, int maxHeight, bool resizeIfWider)
{
try
{
System.Drawing.Image FullSizeImage = System.Drawing.Image.FromFile(origFileLocation);
// Ensure the generated thumbnail is not being used by rotating it 360 degrees
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullSizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (resizeIfWider)
{
if (FullSizeImage.Width <= newWidth)
{
//newWidth = FullSizeImage.Width;
}
}
int newHeight = FullSizeImage.Height * newWidth / FullSizeImage.Width;
if (newHeight > maxHeight) // Height resize if necessary
{
//newWidth = FullSizeImage.Width * maxHeight / FullSizeImage.Height;
newHeight = maxHeight;
}
newHeight = maxHeight;
// Create the new image with the sizes we've calculated
System.Drawing.Image NewImage = FullSizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
FullSizeImage.Dispose();
NewImage.Save(Path.Combine(newFileLocation, newFileName));
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
origFileLocation : Original file location(Source Folder)
newFileLocation : New file location(Destination Folder)
newFileName : new file name to be saved to destination folder
你可以如果您不想要图像大小调整逻辑,则更改该功能
You may alter the function if you do not want image resize logic