Skip to content

Commit

Permalink
Bug/i1626 (#1636)
Browse files Browse the repository at this point in the history
* Added fix for #1626

* Ensured two-anchor and other images are copied properly

* Added fixed issues
  • Loading branch information
OssianEPPlus authored Oct 21, 2024
1 parent e56aff6 commit 997b157
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/articles/fixedissues.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/EPPlus/Drawing/ExcelDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions src/EPPlus/Drawing/ExcelPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions src/EPPlus/FormulaParsing/LexicalAnalysis/FormulaAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions src/EPPlusTest/Drawing/CopyDrawingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using OfficeOpenXml.Drawing;
using System.Linq;

namespace EPPlusTest.Drawing
{
Expand Down Expand Up @@ -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);
}
}
}
}
13 changes: 13 additions & 0 deletions src/EPPlusTest/Issues/LegacyTests/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 997b157

Please sign in to comment.