public static ImageInfo GetImageInformation(string fileName) { ImageInfo result = new ImageInfo(); result.Width = 0; result.Height = 0; result.FormattedDimensions = "unknown"; result.FormattedSize = "unknown"; result.SizeInBytes = 0; string fullName = string.Empty; if (fileName != null) { fullName = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, fileName.Replace("/", "\\")); } if (File.Exists(fullName) == true) { FileInfo f = new FileInfo(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, fileName)); if (f != null) { result.SizeInBytes = f.Length; if (result.SizeInBytes < 1024) { result.FormattedSize = result.SizeInBytes + " bytes"; } else { if (result.SizeInBytes < 1048576) { result.FormattedSize = Math.Round(result.SizeInBytes / 1024, 1) + " KB"; } else { result.FormattedSize = Math.Round(result.SizeInBytes / 1048576, 1) + " MB"; } } } f = null; if (File.Exists(fullName) == true) { System.Drawing.Image WorkingImage; WorkingImage = System.Drawing.Image.FromFile(fullName); if (WorkingImage != null) { result.Width = WorkingImage.Width; result.Height = WorkingImage.Height; result.FormattedDimensions = result.Width.ToString(System.Globalization.CultureInfo.InvariantCulture) + " x " + result.Height.ToString(System.Globalization.CultureInfo.InvariantCulture); } WorkingImage.Dispose(); WorkingImage = null; } } return result; }
public static ImageInfo GetProportionalImageDimensionsForImage(ImageInfo oldInfo, int maxWidth, int maxHeight) { ImageInfo result = new ImageInfo(); if (oldInfo != null) { System.Drawing.Size s = MerchantTribe.Web.Images.GetNewDimensions(maxWidth, maxHeight, oldInfo.Width, oldInfo.Height); result.Height = s.Height; result.Width = s.Width; } return result; }