public static byte[] ResizeImageFile(byte[] imageFile, SueetiePhotoSize size) { byte[] buffer; using (var sourceImage = Image.FromStream(new MemoryStream(imageFile))) { int height; int width; if (size == SueetiePhotoSize.Small || size == SueetiePhotoSize.Medium) { if (size == SueetiePhotoSize.Small) { width = CommerceSettings.Instance.FixedSmallImageWidth; } else { width = CommerceSettings.Instance.FixedMediumImageWidth; } height = (int)(sourceImage.Height * (((float)width) / ((float)sourceImage.Width))); } else if (sourceImage.Height > sourceImage.Width) { height = Math.Min(sourceImage.Height, CommerceSettings.Instance.MaxFullImageSize); width = (int)(sourceImage.Width * (((float)height) / ((float)sourceImage.Height))); } else { width = Math.Min(sourceImage.Width, CommerceSettings.Instance.MaxFullImageSize); height = (int)(sourceImage.Height * (((float)width) / ((float)sourceImage.Width))); } using (var targetImage = Image.FromStream(new MemoryStream(imageFile))) { using (var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb)) { bitmap.SetResolution(72f, 72f); using (var graphics = Graphics.FromImage(bitmap)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.DrawImage(targetImage, new Rectangle(0, 0, width, height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel); MemoryStream stream = new MemoryStream(); bitmap.Save(stream, ImageFormat.Jpeg); buffer = stream.GetBuffer(); } } } } return(buffer); }
public static string GetFilePath(int photoId, bool forUrl, SueetiePhotoSize size, SueetiePhotoType _photoType) { string result = null; string filenameToken; if (size == SueetiePhotoSize.Full) { filenameToken = "Lg"; } else if (size == SueetiePhotoSize.Medium) { filenameToken = "Md"; } else if (size == SueetiePhotoSize.Small) { filenameToken = "Sm"; } else { filenameToken = "Org"; } var uri = HttpContext.Current.Request.Url; var rootUrl = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port + "/"; var uploadPath = @"/images/slideshows/"; if (_photoType == SueetiePhotoType.ProductPhoto) { uploadPath = @"/images/products/"; } if (forUrl) { result = string.Format("{0}/{1}/{2}.{3}.jpg", rootUrl, uploadPath, photoId, filenameToken); } else { var context = HttpContext.Current; if (context != null) { var serverDirectory = context.Server.MapPath(uploadPath); var file = string.Format("{0}.{1}.jpg", photoId, filenameToken); result = Path.Combine(serverDirectory, file); } } return(result); }