品質の指定は、Bitmap.Save() にエンコードパラメータを与えるだけ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System.Drawing.Imaging; private void ConvertJpeg() { // Loads JPEG image. Bitmap bmp1 = new Bitmap(@"C:\Test.jpg"); // Creates the encoder object. Encoder myEncoder = Encoder.Quality; // Creates the encode parameter (Quality=50) // Higher quality compresses image more. // Quality=100 applies the highest compression. EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L); EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = myEncoderParameter; // Saves the bitmap. ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); bmp1.Save(@"C:\TestResult.jpg", jgpEncoder, myEncoderParameters); } private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } |
参考サイト
方法 : JPEG 圧縮レベルの設定
http://msdn.microsoft.com/ja-jp/library/bb882583(v=vs.110).aspx