Just a simple code tips for saving image as JPEG or PNG format in C#.
Save image as JPEG with Quality parameter
public static readonly ImageCodecInfo JPEG_CODEC = GetEncoderInfo("image/jpeg"); public static void SaveImageAsJpeg(this Image src, string fileName, int quality) { var eps = new EncoderParameters(1); var ep = new EncoderParameter(Encoder.Quality, quality); eps.Param[0] = ep; src.Save(fileName.ChangeExtension("jpg"), JPEG_CODEC, eps); } public static ImageCodecInfo GetEncoderInfo(string mineType) { return ImageCodecInfo.GetImageEncoders().First(enc => enc.MimeType == mineType); }
Save image as PNG
public static void SaveImageAsPng(this Image src, string fileName) { src.Save(fileName.ChangeExtension("png"), ImageFormat.Png); }
コメント