コード例 #1
0
        protected void CheckSimpleRenderTileResponse(SKBitmap bitmap, string fileName = "", string compareToFile = "")
        {
            // Convert the response into a bitmap
            bitmap.Should().NotBeNull();

            if (!string.IsNullOrEmpty(fileName))
            {
                using var image  = SKImage.FromBitmap(bitmap);
                using var data   = image.Encode(SKEncodedImageFormat.Png, 100);
                using var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
                data.SaveTo(stream);
            }
            else
            {
                // If the comparison file does not exist then create it to provide a base comparison moving forward.
                if (!string.IsNullOrEmpty(compareToFile) && !File.Exists(compareToFile))
                {
                    using var image  = SKImage.FromBitmap(bitmap);
                    using var data   = image.Encode(SKEncodedImageFormat.Png, 100);
                    using var stream = new FileStream(compareToFile, FileMode.Create, FileAccess.Write, FileShare.None);
                    data.SaveTo(stream);
                }
            }

            if (!string.IsNullOrEmpty(compareToFile))
            {
                var goodBmp = SKBitmap.Decode(compareToFile);

                goodBmp.Height.Should().Be(bitmap.Height);
                goodBmp.Width.Should().Be(bitmap.Width);

                for (var i = 0; i <= bitmap.Width - 1; i++)
                {
                    for (var j = 0; j < bitmap.Height - 1; j++)
                    {
                        goodBmp.GetPixel(i, j).Should().Be(bitmap.GetPixel(i, j));
                    }
                }
            }
        }