private static void createBrokenImage(Stream ostm, ProcessImageSettings s) { s = s.Clone(); if (s.Width == 0 && s.Height == 0) { s.Height = s.Width = 100; } s.Fixup(s.Width > 0 ? s.Width : s.Height, s.Height > 0 ? s.Height : s.Width); using var bmp = new Bitmap(s.Width, s.Height, GdiPixelFormat.Format24bppRgb); using var gfx = Graphics.FromImage(bmp); using var pen = new Pen(Brushes.White, 1.75f); gfx.FillRectangle(Brushes.Gainsboro, new Rectangle(0, 0, s.Width, s.Height)); gfx.SmoothingMode = SmoothingMode.AntiAlias; gfx.PixelOffsetMode = PixelOffsetMode.Half; gfx.CompositingQuality = CompositingQuality.GammaCorrected; float l = 0.5f, t = 0.5f, r = s.Width - 0.5f, b = s.Height - 0.5f; gfx.DrawLines(pen, new[] { new PointF(l, t), new PointF(r, b), new PointF(l, b), new PointF(r, t) }); gfx.DrawLines(pen, new[] { new PointF(l, b), new PointF(l, t), new PointF(r, t), new PointF(r, b) }); bmp.Save(ostm, ImageFormat.Png); }
private static ProcessImageResult processImage(Stream istm, Stream ostm, ProcessImageSettings s) { using (var img = Image.FromStream(istm, true, false)) { if (s.FrameIndex > 0) { var fd = img.RawFormat.Guid == ImageFormat.Gif.Guid ? FrameDimension.Time : FrameDimension.Page; if (img.GetFrameCount(fd) > s.FrameIndex) { img.SelectActiveFrame(fd, s.FrameIndex); } else { throw new ArgumentOutOfRangeException("Invalid Frame Index"); } } img.ExifRotate(); s = s.Clone(); s.Fixup(img.Width, img.Height); bool alpha = ((ImageFlags)img.Flags).HasFlag(ImageFlags.HasAlpha); var mode = s.Interpolation.WeightingFunction.Support <0.1 ? InterpolationMode.NearestNeighbor : s.Interpolation.WeightingFunction.Support <1.0 ? s.ScaleRatio> 1.0 ? InterpolationMode.Bilinear : InterpolationMode.NearestNeighbor : s.Interpolation.WeightingFunction.Support> 1.0 ? s.ScaleRatio > 1.0 || s.Interpolation.Blur > 1.0 ? InterpolationMode.HighQualityBicubic : InterpolationMode.Bicubic : s.ScaleRatio > 1.0 ? InterpolationMode.HighQualityBilinear : InterpolationMode.Bilinear; var src = img; var crop = s.Crop; if (s.HybridScaleRatio > 1d && (mode == InterpolationMode.HighQualityBicubic || mode == InterpolationMode.HighQualityBilinear)) { int intw = (int)Math.Ceiling(img.Width / s.HybridScaleRatio); int inth = (int)Math.Ceiling(img.Height / s.HybridScaleRatio); var bmp = new Bitmap(intw, inth); using (var gfx = Graphics.FromImage(bmp)) { gfx.PixelOffsetMode = PixelOffsetMode.Half; gfx.CompositingMode = CompositingMode.SourceCopy; gfx.DrawImage(img, new Rectangle(0, 0, intw, inth), crop.X, crop.Y, crop.Width, crop.Height, GraphicsUnit.Pixel); } img.Dispose(); src = bmp; crop = new Rectangle(0, 0, intw, inth); } using (src) using (var iat = new ImageAttributes()) using (var bmp = new Bitmap(s.Width, s.Height, alpha ? GdiPixelFormat.Format32bppArgb : GdiPixelFormat.Format24bppRgb)) using (var gfx = Graphics.FromImage(bmp)) { iat.SetWrapMode(WrapMode.TileFlipXY); gfx.PixelOffsetMode = PixelOffsetMode.Half; gfx.CompositingMode = CompositingMode.SourceCopy; gfx.InterpolationMode = mode; if (alpha && !s.MatteColor.IsEmpty) { gfx.Clear(s.MatteColor); gfx.CompositingMode = CompositingMode.SourceOver; gfx.CompositingQuality = CompositingQuality.GammaCorrected; } gfx.DrawImage(src, new Rectangle(0, 0, s.Width, s.Height), crop.X, crop.Y, crop.Width, crop.Height, GraphicsUnit.Pixel, iat); switch (s.SaveFormat) { case FileFormat.Bmp: bmp.Save(ostm, ImageFormat.Bmp); break; case FileFormat.Tiff: using (var encoderParams = new EncoderParameters(1)) using (var param = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone)) { encoderParams.Param[0] = param; bmp.Save(ostm, tiffCodec, encoderParams); } break; case FileFormat.Jpeg: using (var encoderParams = new EncoderParameters(1)) using (var param = new EncoderParameter(Encoder.Quality, s.JpegQuality)) { encoderParams.Param[0] = param; bmp.Save(ostm, jpegCodec, encoderParams); } break; default: if (s.IndexedColor) { bmp.Save(ostm, ImageFormat.Gif); } else { bmp.Save(ostm, ImageFormat.Png); } break; } } } return(new ProcessImageResult { Settings = s, Stats = Enumerable.Empty <PixelSourceStats>() }); }
private static ProcessImageResult processImage(Stream istm, Stream ostm, ProcessImageSettings s) { using var img = Image.FromStream(istm, s.ColorProfileMode != ColorProfileMode.Ignore, false); if (s.FrameIndex > 0) { var fd = img.RawFormat.Guid == ImageFormat.Gif.Guid ? FrameDimension.Time : FrameDimension.Page; if (img.GetFrameCount(fd) > s.FrameIndex) { img.SelectActiveFrame(fd, s.FrameIndex); } else { throw new ArgumentOutOfRangeException(nameof(s), "Invalid Frame Index"); } } if (s.OrientationMode == OrientationMode.Normalize) { img.ExifRotate(); } s = s.Clone(); s.Fixup(img.Width, img.Height); var usedSettings = s.Clone(); bool alpha = ((ImageFlags)img.Flags).HasFlag(ImageFlags.HasAlpha); var pixelFormat = alpha && s.MatteColor.IsTransparent() ? GdiPixelFormat.Format32bppArgb : GdiPixelFormat.Format24bppRgb; var mode = s.Interpolation.WeightingFunction.Support <0.1 ? InterpolationMode.NearestNeighbor : s.Interpolation.WeightingFunction.Support <1.0 ? s.ScaleRatio> 1.0 ? InterpolationMode.Bilinear : InterpolationMode.NearestNeighbor : s.Interpolation.WeightingFunction.Support> 1.0 ? s.ScaleRatio > 1.0 || s.Interpolation.Blur > 1.0 ? InterpolationMode.HighQualityBicubic : InterpolationMode.Bicubic : s.ScaleRatio > 1.0 ? InterpolationMode.HighQualityBilinear : InterpolationMode.Bilinear; using var src = img.HybridScale(s, mode); using var iat = new ImageAttributes(); using var bmp = new Bitmap(s.Width, s.Height, pixelFormat); using var gfx = Graphics.FromImage(bmp); iat.SetWrapMode(WrapMode.TileFlipXY); gfx.PixelOffsetMode = PixelOffsetMode.Half; gfx.CompositingMode = CompositingMode.SourceCopy; gfx.InterpolationMode = mode; if ((alpha || s.InnerSize != s.OuterSize) && !s.MatteColor.IsEmpty) { gfx.Clear(s.MatteColor); gfx.CompositingMode = CompositingMode.SourceOver; gfx.CompositingQuality = CompositingQuality.GammaCorrected; } gfx.DrawImage(src, s.InnerRect, s.Crop.X, s.Crop.Y, s.Crop.Width, s.Crop.Height, GraphicsUnit.Pixel, iat); switch (s.SaveFormat) { case FileFormat.Bmp: bmp.Save(ostm, ImageFormat.Bmp); break; case FileFormat.Tiff: using (var encoderParams = new EncoderParameters(1)) using (var param = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone)) { encoderParams.Param[0] = param; bmp.Save(ostm, tiffCodec, encoderParams); } break; case FileFormat.Jpeg: using (var encoderParams = new EncoderParameters(1)) using (var param = new EncoderParameter(Encoder.Quality, s.JpegQuality)) { encoderParams.Param[0] = param; bmp.Save(ostm, jpegCodec, encoderParams); } break; default: if (s.IndexedColor) { bmp.Save(ostm, ImageFormat.Gif); } else { bmp.Save(ostm, ImageFormat.Png); } break; } return(new ProcessImageResult(usedSettings, Enumerable.Empty <PixelSourceStats>())); }
public WicProcessingContext(ProcessImageSettings settings) { Settings = settings.Clone(); }