diff --git a/docs/articles/fixedissues.md b/docs/articles/fixedissues.md index 552d3756b..2cc7b0c0b 100644 --- a/docs/articles/fixedissues.md +++ b/docs/articles/fixedissues.md @@ -1,4 +1,10 @@ # Features / Fixed issues - EPPlus 7 + + + + + +* Ensured ExcelPictures are sized correctly when copying worksheets ## Version 7.4.1 * Updated for vulnerability in System.Text.Json 8.0.4 - Microsoft.Extensions.Configuration.Json 8.0.0 -> 8.0.1 diff --git a/src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs b/src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs index 8cc53b1d7..d9b4185ab 100644 --- a/src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs +++ b/src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs @@ -343,7 +343,12 @@ internal static void CopyDrawing(ExcelWorksheet source, ExcelWorksheet target) //The slicer still reference the copied slicers cache. We need to create a new cache for the copied slicer. ptSlicer.CreateNewCache(((ExcelPivotTableSlicer)draw).Cache._field); } - + else if (c is ExcelPicture pic) + { + var origPic = (ExcelPicture)draw; + pic.SetPixelWidth(origPic.GetPixelWidth()); + pic.SetPixelHeight(origPic.GetPixelHeight()); + } } } diff --git a/src/EPPlus/Drawing/ExcelDrawing.cs b/src/EPPlus/Drawing/ExcelDrawing.cs index 9419239ac..4bd4b0ba9 100644 --- a/src/EPPlus/Drawing/ExcelDrawing.cs +++ b/src/EPPlus/Drawing/ExcelDrawing.cs @@ -577,7 +577,6 @@ internal static ExcelDrawing GetDrawingFromNode(ExcelDrawings drawings, XmlNode return GetShapeOrControl(drawings, node, drawNode, parent); case "pic": var aPic = new ExcelPicture(drawings, node, parent); - aPic.RecalcWidthHeight(); return aPic; case "graphicFrame": return ExcelChart.GetChart(drawings, node, parent); diff --git a/src/EPPlus/Drawing/ExcelPicture.cs b/src/EPPlus/Drawing/ExcelPicture.cs index f5f2409ef..857faa6ec 100644 --- a/src/EPPlus/Drawing/ExcelPicture.cs +++ b/src/EPPlus/Drawing/ExcelPicture.cs @@ -19,11 +19,8 @@ Date Author Change using OfficeOpenXml.Drawing.Style.Effect; using OfficeOpenXml.Packaging; using System.Linq; -using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions; using System.Globalization; - - #if NETFULL using System.Drawing.Imaging; #endif @@ -268,12 +265,18 @@ private void SaveImageToPackage(ePictureType type, byte[] img) internal void RecalcWidthHeight() { + //Ensure image has a size.width and size.height based on 100% orignal image if(Image != null && Image.ImageBytes != null) { + //Recalculates width/height and bounds to 100% width/height relative to original image size Image.Bounds = PictureStore.GetImageBounds(Image.ImageBytes, Image.Type.Value, _drawings._package); + var width = Image.Bounds.Width / (Image.Bounds.HorizontalResolution / STANDARD_DPI); var height = Image.Bounds.Height / (Image.Bounds.VerticalResolution / STANDARD_DPI); SetPosDefaults((float)width, (float)height); + + //Image.Bounds.Height = origHeight; + //Image.Bounds.Width = origWidth; } } @@ -295,14 +298,20 @@ private void AddNewPicture(byte[] img, string relID) #endregion private void SetPosDefaults(float width, float height) { - if(EditAs != eEditAs.Absolute) + var prevEdit = EditAs; + + if (EditAs != eEditAs.Absolute) { EditAs = eEditAs.OneCell; } + SetPixelWidth(width); SetPixelHeight(height); + _width = GetPixelWidth(); _height = GetPixelHeight(); + + EditAs = prevEdit; } internal void SetNewId(int newId) diff --git a/src/EPPlus/FormulaParsing/LexicalAnalysis/FormulaAddress.cs b/src/EPPlus/FormulaParsing/LexicalAnalysis/FormulaAddress.cs index f5048454a..54d33ce77 100644 --- a/src/EPPlus/FormulaParsing/LexicalAnalysis/FormulaAddress.cs +++ b/src/EPPlus/FormulaParsing/LexicalAnalysis/FormulaAddress.cs @@ -5,13 +5,10 @@ using System.Text; using static OfficeOpenXml.ExcelAddressBase; using OfficeOpenXml.Core.CellStore; -using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup; using System.Globalization; -using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions; using OfficeOpenXml.FormulaParsing.Excel.Functions; using System.Linq; using OfficeOpenXml.Utils; -using System.Net; using OfficeOpenXml.FormulaParsing.Ranges; namespace OfficeOpenXml.FormulaParsing.LexicalAnalysis diff --git a/src/EPPlusTest/Drawing/CopyDrawingTests.cs b/src/EPPlusTest/Drawing/CopyDrawingTests.cs index abcdd8cf4..45b6e4e9d 100644 --- a/src/EPPlusTest/Drawing/CopyDrawingTests.cs +++ b/src/EPPlusTest/Drawing/CopyDrawingTests.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using OfficeOpenXml.Drawing; +using System.Linq; namespace EPPlusTest.Drawing { @@ -394,5 +395,53 @@ public void AddAndCopyImage() SaveAndCleanup(package); } } + + [TestMethod] + public void AddAndCopyImageWithout100Size() + { + using (var package = OpenPackage("AddPic50percent.xlsx", true)) + { + var sheet = package.Workbook.Worksheets.Add("emptyWS"); + var uri = GetResourceFile("EPPlus.png").FullName; + + var pic = sheet.Drawings.AddPicture("ImageName", uri); + + pic.SetSize(50); + + var copiedWs = package.Workbook.Worksheets.Copy("emptyWS", "Copy"); + var picCopied = (ExcelPicture)copiedWs.Drawings[0]; + + Assert.AreEqual(pic._width, picCopied._width); + Assert.AreEqual(pic.Size.Width, picCopied.Size.Width); + + SaveAndCleanup(package); + } + } + + [TestMethod] + public void ReadAndCopyTwoAnchorImage() + { + using (var package = OpenTemplatePackage("SizeCopyTest.xlsx")) + { + var sheet = package.Workbook.Worksheets.First(); + + var pic = sheet.Drawings.First(); + + sheet.Drawings.ReadPositionsAndSize(); + + var copiedWs = package.Workbook.Worksheets.Copy(sheet.Name, "Copy"); + var picCopied = (ExcelPicture)copiedWs.Drawings[0]; + + Assert.AreEqual(pic._width, picCopied._width); + Assert.AreEqual(pic._height, picCopied._height); + + Assert.AreEqual(pic.From.Row, picCopied.From.Row); + Assert.AreEqual(pic.From.Column, picCopied.From.Column); + Assert.AreEqual(pic.To.Row, picCopied.To.Row); + Assert.AreEqual(pic.To.Column, picCopied.To.Column); + + SaveAndCleanup(package); + } + } } } diff --git a/src/EPPlusTest/Issues/LegacyTests/Issues.cs b/src/EPPlusTest/Issues/LegacyTests/Issues.cs index ac756390e..e93fcfd61 100644 --- a/src/EPPlusTest/Issues/LegacyTests/Issues.cs +++ b/src/EPPlusTest/Issues/LegacyTests/Issues.cs @@ -6218,6 +6218,19 @@ public void s660() Assert.AreEqual(" 10.01", cellValue.Value); + SaveAndCleanup(package); + } + } + [TestMethod] + public void i1626() + { + using (var package = OpenTemplatePackage("i1626.xlsx")) + { + var sheet = package.Workbook.Worksheets[0]; + + var pictures = sheet.Drawings.Where(x => x.DrawingType == eDrawingType.Picture).Select(x => x.As.Picture); + var pic = pictures.First(); + SaveAndCleanup(package); } }