public void ToStream(Stream output, string format, int qualityPercent = 100) { BitmapEncoder encoder = EncoderSelector.GetEncoder(format); JpegBitmapEncoder jpegEncoder = encoder as JpegBitmapEncoder; if (jpegEncoder != null) { jpegEncoder.QualityLevel = qualityPercent; } else if (qualityPercent != 100) { throw new ArgumentException("Can't change quality for this format"); } encoder.Frames.Add(BitmapFrame.Create(Target)); encoder.Save(output); }
public unsafe void ToStream(Stream output, string format) { format = format.ToLower().Replace(".", ""); if (format != "gif") // TODO: Can't set delay to BitmapEncoder { BitmapEncoder encoder = EncoderSelector.GetEncoder(format); foreach (IImage image in this) { BitmapSource bitmapSource = image.ToBitmapSource(); encoder.Frames.Add(BitmapFrame.Create(bitmapSource)); } encoder.Save(output); } else { AnimatedGifEncoder encoder = new AnimatedGifEncoder(); encoder.SetQuality(100); encoder.SetRepeat(0); encoder.Start(output); foreach (IImage image in this) { IntPtr ptr = (IntPtr)image.BeginPixelWorking(false); Bitmap frame = new Bitmap(image.Width, image.Height, image.Width * 4, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, ptr); image.EndPixelWorking(); encoder.SetDelay(image.AnimationDelay); encoder.AddFrame(frame); frame.Dispose(); } encoder.Finish(); } }