// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; using PixelFormat = System.Drawing.Imaging.PixelFormat; namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs { public class SystemDrawingReferenceDecoder : IImageDecoder { public static SystemDrawingReferenceDecoder Instance { get; } = new SystemDrawingReferenceDecoder(); public Image Decode(Configuration configuration, Stream stream) where TPixel : struct, IPixel { using (var sourceBitmap = new System.Drawing.Bitmap(stream)) { if (sourceBitmap.PixelFormat == PixelFormat.Format32bppArgb) { return SystemDrawingBridge.FromFromArgb32SystemDrawingBitmap(sourceBitmap); } using (var convertedBitmap = new System.Drawing.Bitmap( sourceBitmap.Width, sourceBitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (var g = Graphics.FromImage(convertedBitmap)) { g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(sourceBitmap, 0, 0, sourceBitmap.Width, sourceBitmap.Height); } return SystemDrawingBridge.FromFromArgb32SystemDrawingBitmap(convertedBitmap); } } } public PixelTypeInfo DetectPixelType(Configuration configuration, Stream stream) { using (var sourceBitmap = new System.Drawing.Bitmap(stream)) { return new PixelTypeInfo(System.Drawing.Image.GetPixelFormatSize(sourceBitmap.PixelFormat)); } } } }