/// <summary> /// Saves an image as a jpeg image, with the given quality /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, Image img, PictureQuality quality) { // Encoder parameter for image quality EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, (int)quality); // JPEG image codec ImageCodecInfo jpgCodec = GetEncoderInfo("image/jpeg"); EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; img.Save(path, jpgCodec, encoderParams); }
/// <summary> /// Saves an image as a jpeg image, with the given quality and size (keeps original ratio) /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, Image img, PictureQuality quality, int maxWidth, int maxHeight) { double ratio = CalculateImageRatio(img, maxWidth, maxHeight); int newWidth = (int)(img.Width * ratio); int newHeight = (int)(img.Height * ratio); var newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.DrawImage(img, 0, 0, newWidth, newHeight); } SaveJpeg(path, newImage, quality); }
/// <summary> /// Saves an image as a jpeg image, with the given quality /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, Stream stream, PictureQuality quality) { Image img = Image.FromStream(stream); SaveJpeg(path, img, quality); }
/// <summary> /// Saves an image as a jpeg image, with the given quality /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, IntPtr hBitmap, PictureQuality quality) { Image img = Image.FromHbitmap(hBitmap); SaveJpeg(path, img, quality); }
/// <summary> /// Saves an image as a jpeg image, with the given quality and size (keeps original ratio) /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, Stream stream, PictureQuality quality, int maxWidth, int maxHeight) { Image img = Image.FromStream(stream); SaveJpeg(path, img, quality, maxWidth, maxHeight); }
/// <summary> /// Saves an image as a jpeg image, with the given quality and size (keeps original ratio) /// </summary> /// <param name="path"> Path to which the image would be saved. </param> /// <param name="quality"> An Enumeration based on 0 to 100, with 100 being the highest quality. </param> public static void SaveJpeg(string path, IntPtr hBitmap, PictureQuality quality, int maxWidth, int maxHeight) { Image img = Image.FromHbitmap(hBitmap); SaveJpeg(path, img, quality, maxWidth, maxHeight); }