/// <summary> /// Save Image in specified picture size /// </summary> /// <param name="filename">Local file path</param> /// <param name="file">Image stream</param> /// <param name="pictureSize">Size of image</param> /// <returns></returns> private WebImage SaveImage(string filename, Stream file, PictureSizes pictureSize) { WebImage result = null; try { result = new WebImage(file); int width = result.Width; int height = result.Height; Size newSize = GetImageSize(pictureSize, width, height); result.Resize(newSize.Width, newSize.Height); RemoveFile(filename); // save the file. result.Save(filename); result.FileName = filename; } catch (Exception) { result = null; } return(result); }
private Size GetImageSize(PictureSizes size, int width, int height) { int newWidth = 0; int newHeight = 0; switch (size) { case PictureSizes.MainPicture: newWidth = 300; newHeight = 300; break; case PictureSizes.StandartSliderPicture: if (width > height) { newWidth = 600; newHeight = newWidth * height / width; newHeight = newHeight > 400 ? 400 : newHeight; } else { newHeight = 400; newWidth = newHeight * width / height; } break; case PictureSizes.SmallSliderPicture: if (width > height) { newWidth = 72; newHeight = newWidth * height / width; } else { newHeight = 72; newWidth = newHeight * width / height; } break; } return(new Size(newWidth, newHeight));; }
/// <summary> /// Save Image in specified picture size /// </summary> /// <param name="filename">Local file path</param> /// <param name="file">Image stream</param> /// <param name="pictureSize">Size of image</param> /// <returns></returns> private WebImage SaveImage(string filename, Stream file, PictureSizes pictureSize) { WebImage result = null; try { result = new WebImage(file); int width = result.Width; int height = result.Height; Size newSize = GetImageSize(pictureSize, width, height); result.Resize(newSize.Width, newSize.Height); RemoveFile(filename); // save the file. result.Save(filename); result.FileName = filename; } catch (Exception) { result = null; } return result; }
private Size GetImageSize(PictureSizes size, int width, int height) { int newWidth = 0; int newHeight = 0; switch (size) { case PictureSizes.MainPicture: newWidth = 300; newHeight = 300; break; case PictureSizes.StandartSliderPicture: if (width > height) { newWidth = 600; newHeight = newWidth * height / width; newHeight = newHeight > 400 ? 400 : newHeight; } else { newHeight = 400; newWidth = newHeight * width / height; } break; case PictureSizes.SmallSliderPicture: if (width > height) { newWidth = 72; newHeight = newWidth * height / width; } else { newHeight = 72; newWidth = newHeight * width / height; } break; } return new Size(newWidth, newHeight); ; }