diff --git a/SpreadCheetah.Test/Images/red-1x1.invalidpng b/SpreadCheetah.Test/Images/red-1x1.invalidpng new file mode 100644 index 00000000..4e628542 Binary files /dev/null and b/SpreadCheetah.Test/Images/red-1x1.invalidpng differ diff --git a/SpreadCheetah.Test/Images/yellow-500x500-transparent.png b/SpreadCheetah.Test/Images/yellow-500x500-transparent.png new file mode 100644 index 00000000..f35ec870 Binary files /dev/null and b/SpreadCheetah.Test/Images/yellow-500x500-transparent.png differ diff --git a/SpreadCheetah.Test/Tests/SpreadsheetImageTests.cs b/SpreadCheetah.Test/Tests/SpreadsheetImageTests.cs index 741c2e3a..f936a173 100644 --- a/SpreadCheetah.Test/Tests/SpreadsheetImageTests.cs +++ b/SpreadCheetah.Test/Tests/SpreadsheetImageTests.cs @@ -139,6 +139,22 @@ public async Task Spreadsheet_EmbedImage_PngFromReadOnlyStream() Assert.Null(exception); } + [Fact] + public async Task Spreadsheet_EmbedImage_PngWithInvalidResolution() + { + // Arrange + using var pngStream = EmbeddedResources.GetStream("red-1x1.invalidpng"); + using var outputStream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(outputStream); + + // Act + var exception = await Record.ExceptionAsync(() => spreadsheet.EmbedImageAsync(pngStream).AsTask()); + + // Assert + var concreteException = Assert.IsType(exception); + Assert.Contains("width", concreteException.Message, StringComparison.OrdinalIgnoreCase); + } + [Fact] public async Task Spreadsheet_AddImage_Png() { @@ -170,6 +186,37 @@ public async Task Spreadsheet_AddImage_Png() Assert.Equal("Image 1", picture.Name); } + [Fact] + public async Task Spreadsheet_AddImage_PngWithTransparentBackground() + { + // Arrange + const string reference = "B3"; + using var pngStream = EmbeddedResources.GetStream("yellow-500x500-transparent.png"); + using var outputStream = new MemoryStream(); + await using var spreadsheet = await Spreadsheet.CreateNewAsync(outputStream); + var embeddedImage = await spreadsheet.EmbedImageAsync(pngStream); + await spreadsheet.StartWorksheetAsync("Sheet 1"); + var canvas = ImageCanvas.OriginalSize(reference.AsSpan()); + + // Act + spreadsheet.AddImage(canvas, embeddedImage); + + // Assert + await spreadsheet.FinishAsync(); + SpreadsheetAssert.Valid(outputStream); + + using var workbook = new XLWorkbook(outputStream); + var worksheet = Assert.Single(workbook.Worksheets); + var picture = Assert.Single(worksheet.Pictures); + Assert.Equal(reference, picture.TopLeftCell.Address.ToString()); + Assert.Equal(XLPictureFormat.Png, picture.Format); + Assert.Equal(500, picture.OriginalWidth); + Assert.Equal(500, picture.OriginalHeight); + Assert.Equal(0, picture.Top); + Assert.Equal(0, picture.Left); + Assert.Equal("Image 1", picture.Name); + } + [Fact] public async Task Spreadsheet_AddImage_PngWithLargeResolution() {