private static string GetNewImageName(string path, string oldname, imageSize size) { string newName = Path.GetFileNameWithoutExtension(path + oldname); FileInfo info = new FileInfo(path + oldname); switch (size) { case imageSize.Mini: newName += "_mini" + info.Extension; break; case imageSize.Thumbnail: newName += "_tn" + info.Extension; break; case imageSize.Small: newName += "_sm" + info.Extension; break; case imageSize.Medium: newName += "_med" + info.Extension; break; case imageSize.Large: newName += "_large" + info.Extension; break; default: newName += info.Extension; break; } return(newName); }
public static string ImageOtherSize(this string locationStore, imageSize seze) { var a = locationStore; if (a.EndsWith(".jpg")) { a = a.Insert(a.Length - (".jpg").Length, "-" + seze.ToString()); } return(a); }
/// <summary> /// Saves the image to disk. /// </summary> /// <param name="image">The image to save.</param> /// <param name="targetSize">Target size of the image to save.</param> /// <param name="path">The path where the image will be saved.</param> /// <param name="oldName">The previous name of the image.</param> /// <returns></returns> public static string SaveImageToDisk(this Image image, imageSize targetSize, string path, string oldName) { Throw.IfArgumentNull(image, "image"); Throw.IfArgumentNullOrEmpty(path, "path"); Throw.IfArgumentNullOrEmpty(oldName, "oldName"); string newName = GetNewImageName(path, oldName, targetSize); //get the codec needed var i = ImageCodecInfo.GetImageEncoders(); var imgCodec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == image.RawFormat.Guid); if (imgCodec == null) { imgCodec = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/jpeg"); } //make a paramater to adjust quality var codecParams = new EncoderParameters(1); //reduce to quality of 80 (from range of 0 (max compression) to 100 (no compression)) codecParams.Param[0] = new EncoderParameter(Encoder.Quality, 80L); image.Save(Path.Combine(path, newName), imgCodec, codecParams); return(Path.Combine(path, newName)); }
public static byte[] imgTosmall(this byte[] byteImageIn, imageSize tosize = imageSize.s800, long dpi = 72) { if (byteImageIn == null) { return(null); } byte[] currentByteImageArray = byteImageIn; MemoryStream inputMemoryStream = new MemoryStream(byteImageIn); Image fullsizeImage = Image.FromStream(inputMemoryStream); double s = (double)(800 * 600); if (fullsizeImage.Width * fullsizeImage.Height > s) { double ww = (double)fullsizeImage.Width / (int)tosize; double hh = (double)fullsizeImage.Height / (int)tosize; var w = fullsizeImage.Width / hh; var h = fullsizeImage.Height / ww; Bitmap fullSizeBitmap = new Bitmap(fullsizeImage, new Size((int)(w), (int)(h))); MemoryStream resultStream = new MemoryStream(); fullSizeBitmap.Save(resultStream, fullsizeImage.RawFormat); currentByteImageArray = resultStream.ToArray(); resultStream.Dispose(); resultStream.Close(); } return(imgTosmallChengeDPI(currentByteImageArray, dpi)); }