public void Save(Stream stream, AzosImageFormat format) { var(codec, pars) = getEncoder(format); using (var copy = makeSaveCopy()) copy.Save(stream, codec, pars); }
private (ImageCodecInfo codec, EncoderParameters pars) getEncoder(AzosImageFormat format) { ImageCodecInfo codec; EncoderParameters pars; if (format is BitmapImageFormat) { codec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Bmp.Guid); pars = new EncoderParameters(1); pars.Param[0] = new EncoderParameter(Encoder.ColorDepth, format.Colors); } else if (format is PngImageFormat) { codec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Png.Guid); pars = null;//new EncoderParameters(0); } else if (format is GifImageFormat) { codec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Gif.Guid); pars = null;//new EncoderParameters(0); } else//default is JPEG { var jpeg = format as JpegImageFormat; codec = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == System.Drawing.Imaging.ImageFormat.Jpeg.Guid); pars = new EncoderParameters(1); pars.Param[0] = new EncoderParameter(Encoder.Quality, jpeg?.Quality ?? 80L); } return(codec : codec, pars : pars); }
public void Save(string fileName, AzosImageFormat format) { var(codec, pars) = getEncoder(format); using (var copy = makeSaveCopy()) copy.Save(fileName, codec, pars); }
public byte[] Save(AzosImageFormat format) { using (var ms = new MemoryStream()) { this.Save(ms, format); return(ms.ToArray()); } }