Switch EPPlus to .NET Standard 2.0 This change required backporting changes from a newer EPPlus version: https://github.com/JanKallman/EPPlus/commit/ 2a0283ce04fcf9e598711c97efeff9318d7a1156 Specifically, ImageConverter (which is not .NET Standard compatible) has been replaced with an EPPlus-specific ImageCompat utility. Change-Id: Ia0a5bda9e93d97f1fad417a3a3889b0f8b69ec2b
diff --git a/EPPlus/Compatibility/ImageCompat.cs b/EPPlus/Compatibility/ImageCompat.cs new file mode 100644 index 0000000..ec0eef3 --- /dev/null +++ b/EPPlus/Compatibility/ImageCompat.cs
@@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; +using System.Text; + +namespace OfficeOpenXml.Compatibility +{ + internal class ImageCompat + { + internal static byte[] GetImageAsByteArray(Image image) + { + var ms = new MemoryStream(); + if (image.RawFormat.Guid == ImageFormat.Gif.Guid) + { + image.Save(ms, ImageFormat.Gif); + } + else if (image.RawFormat.Guid == ImageFormat.Bmp.Guid) + { + image.Save(ms, ImageFormat.Bmp); + } + else if (image.RawFormat.Guid == ImageFormat.Png.Guid) + { + image.Save(ms, ImageFormat.Png); + } + else if (image.RawFormat.Guid == ImageFormat.Tiff.Guid) + { + image.Save(ms, ImageFormat.Tiff); + } + else + { + image.Save(ms, ImageFormat.Jpeg); + } + + return ms.ToArray(); + } + } +}
diff --git a/EPPlus/Drawing/ExcelPicture.cs b/EPPlus/Drawing/ExcelPicture.cs index 368a8b4..59e8cf1 100644 --- a/EPPlus/Drawing/ExcelPicture.cs +++ b/EPPlus/Drawing/ExcelPicture.cs
@@ -13,17 +13,17 @@ * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html * - * All code and executables are provided "as is" with no warranty either express or implied. + * All code and executables are provided "as is" with no warranty either express or implied. * The author accepts no liability for any damage or loss of business that this product may cause. * * Code change notes: - * + * * Author Change Date * ****************************************************************************** * Jan Källman Initial Release 2009-10-01 @@ -38,6 +38,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.Diagnostics; +using OfficeOpenXml.Compatibility; using OfficeOpenXml.Utils; namespace OfficeOpenXml.Drawing @@ -61,8 +62,12 @@ FileInfo f = new FileInfo(UriPic.OriginalString); ContentType = GetContentType(f.Extension); _image = Image.FromStream(Part.GetStream()); +#if (Core) + byte[] iby = ImageCompat.GetImageAsByteArray(_image); +#else ImageConverter ic=new ImageConverter(); var iby=(byte[])ic.ConvertTo(_image, typeof(byte[])); +#endif var ii = _drawings._package.LoadImage(iby, UriPic, Part); ImageHash = ii.Hash; @@ -126,8 +131,12 @@ ContentType = GetContentType(imageFile.Extension); var imagestream = new FileStream(imageFile.FullName, FileMode.Open, FileAccess.Read); _image = Image.FromStream(imagestream); +#if (Core) + var img=ImageCompat.GetImageAsByteArray(_image); +#else ImageConverter ic = new ImageConverter(); var img = (byte[])ic.ConvertTo(_image, typeof(byte[])); +#endif imagestream.Close(); UriPic = GetNewUri(package, "/xl/media/{0}" + imageFile.Name); @@ -198,8 +207,12 @@ #endregion private string SavePicture(Image image) { +#if (Core) + byte[] img = ImageCompat.GetImageAsByteArray(image); +#else ImageConverter ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(image, typeof(byte[])); +#endif var ii = _drawings._package.AddImage(img); ImageHash = ii.Hash; @@ -218,7 +231,7 @@ //Set the Image and save it to the package. RelPic = _drawings.Part.CreateRelationship(UriHelper.GetRelativeUri(_drawings.UriDrawing, UriPic), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image"); - + //AddNewPicture(img, picRelation.Id); _drawings._hashes.Add(ii.Hash, RelPic.Id); @@ -234,9 +247,9 @@ private string PicStartXml() { StringBuilder xml = new StringBuilder(); - + xml.Append("<xdr:nvPicPr>"); - + if (_hyperlink == null) { xml.AppendFormat("<xdr:cNvPr id=\"{0}\" descr=\"\" />", _id); @@ -260,7 +273,7 @@ } xml.Append("</xdr:cNvPr>"); } - + xml.Append("<xdr:cNvPicPr><a:picLocks noChangeAspect=\"1\" /></xdr:cNvPicPr></xdr:nvPicPr><xdr:blipFill><a:blip xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:embed=\"\" cstate=\"print\" /><a:stretch><a:fillRect /> </a:stretch> </xdr:blipFill> <xdr:spPr> <a:xfrm> <a:off x=\"0\" y=\"0\" /> <a:ext cx=\"0\" cy=\"0\" /> </a:xfrm> <a:prstGeom prst=\"rect\"> <a:avLst /> </a:prstGeom> </xdr:spPr>"); return xml.ToString(); @@ -271,7 +284,7 @@ /// <summary> /// The Image /// </summary> - public Image Image + public Image Image { get { @@ -288,7 +301,7 @@ //Create relationship TopNode.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", NameSpaceManager).Value = relID; - //_image.Save(Part.GetStream(FileMode.Create, FileAccess.Write), _imageFormat); //Always JPEG here at this point. + //_image.Save(Part.GetStream(FileMode.Create, FileAccess.Write), _imageFormat); //Always JPEG here at this point. } catch(Exception ex) { @@ -402,7 +415,7 @@ base.Dispose(); _hyperlink = null; _image.Dispose(); - _image = null; + _image = null; } } -} \ No newline at end of file +}
diff --git a/EPPlus/EPPlusSDK.csproj b/EPPlus/EPPlusSDK.csproj index b2ec784..42fc4ae 100644 --- a/EPPlus/EPPlusSDK.csproj +++ b/EPPlus/EPPlusSDK.csproj
@@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> - <TargetFrameworks>net472;net5.0</TargetFrameworks> + <TargetFramework>netstandard2.0</TargetFramework> <RootNamespace>OfficeOpenXml</RootNamespace> <AssemblyName>EPPlus</AssemblyName> <SignAssembly>true</SignAssembly> @@ -8,6 +8,7 @@ <PackageId>Appsheet.EPPlus</PackageId> <Version>1.0.0</Version> <DefaultItemExcludes>$(DefaultItemExcludes);Properties/AssemblyInfo.cs;FormulaParsing/LexicalAnalysis/TokenSeparatorHandlers/**;FormulaParsing/LexicalAnalysis/TokenHandler.cs;FormulaParsing/Excel/Functions/Math/Rank.cs</DefaultItemExcludes> + <DefineConstants>Core;STANDARD20</DefineConstants> </PropertyGroup> <ItemGroup Condition="'$(TargetFramework)' == 'net472'"> <Reference Include="System.Web" />
diff --git a/EPPlus/ExcelBackgroundImage.cs b/EPPlus/ExcelBackgroundImage.cs index 8c069f7..601cf09 100644 --- a/EPPlus/ExcelBackgroundImage.cs +++ b/EPPlus/ExcelBackgroundImage.cs
@@ -13,17 +13,17 @@ * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html * - * All code and executables are provided "as is" with no warranty either express or implied. + * All code and executables are provided "as is" with no warranty either express or implied. * The author accepts no liability for any damage or loss of business that this product may cause. * * Code change notes: - * + * * Author Change Date ******************************************************************************* * Jan Källman Added 10-SEP-2009 @@ -36,6 +36,7 @@ using System.Xml; using System.Drawing; using System.IO; +using OfficeOpenXml.Compatibility; using OfficeOpenXml.Drawing; using OfficeOpenXml.Packaging; using OfficeOpenXml.Utils; @@ -48,7 +49,7 @@ { ExcelWorksheet _workSheet; /// <summary> - /// + /// /// </summary> /// <param name="nsm"></param> /// <param name="topNode">The topnode of the worksheet</param> @@ -58,10 +59,10 @@ { _workSheet = workSheet; } - + const string BACKGROUNDPIC_PATH = "d:picture/@r:id"; /// <summary> - /// The background image of the worksheet. + /// The background image of the worksheet. /// The image will be saved internally as a jpg. /// </summary> public Image Image @@ -86,8 +87,12 @@ } else { +#if (Core) + var img=ImageCompat.GetImageAsByteArray(value); +#else ImageConverter ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(value, typeof(byte[])); +#endif var ii = _workSheet.Workbook._package.AddImage(img); var rel = _workSheet.Part.CreateRelationship(ii.Uri, Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/image"); SetXmlNodeString(BACKGROUNDPIC_PATH, rel.Id); @@ -95,7 +100,7 @@ } } /// <summary> - /// Set the picture from an image file. + /// Set the picture from an image file. /// The image file will be saved as a blob, so make sure Excel supports the image format. /// </summary> /// <param name="PictureFile">The image file.</param> @@ -113,14 +118,16 @@ throw (new InvalidDataException("File is not a supported image-file or is corrupt", ex)); } - ImageConverter ic = new ImageConverter(); string contentType = ExcelPicture.GetContentType(PictureFile.Extension); var imageURI = XmlHelper.GetNewUri(_workSheet._package.Package, "/xl/media/" + PictureFile.Name.Substring(0, PictureFile.Name.Length - PictureFile.Extension.Length) + "{0}" + PictureFile.Extension); - +#if (Core) + var fileBytes=ImageCompat.GetImageAsByteArray(img); +#else + var ic = new ImageConverter(); byte[] fileBytes = (byte[])ic.ConvertTo(img, typeof(byte[])); +#endif var ii = _workSheet.Workbook._package.AddImage(fileBytes, imageURI, contentType); - if (_workSheet.Part.Package.PartExists(imageURI) && ii.RefCount==1) //The file exists with another content, overwrite it. { //Remove the part if it exists @@ -141,13 +148,17 @@ var relID = GetXmlNodeString(BACKGROUNDPIC_PATH); if (relID != "") { +#if (Core) + var img=ImageCompat.GetImageAsByteArray(Image); +#else var ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(Image, typeof(byte[])); +#endif var ii = _workSheet.Workbook._package.GetImageInfo(img); //Delete the relation _workSheet.Part.DeleteRelationship(relID); - + //Delete the image if there are no other references. if (ii != null && ii.RefCount == 1) { @@ -156,7 +167,7 @@ _workSheet.Part.Package.DeletePart(ii.Uri); } } - + } } }
diff --git a/EPPlus/ExcelHeaderFooter.cs b/EPPlus/ExcelHeaderFooter.cs index e756a33..3f978c4 100644 --- a/EPPlus/ExcelHeaderFooter.cs +++ b/EPPlus/ExcelHeaderFooter.cs
@@ -4,7 +4,7 @@ * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets. * See http://www.codeplex.com/EPPlus for details. * - * Copyright (C) 2011 Jan Källman + * Copyright (C) 2011 Jan Källman * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,9 +26,9 @@ * * Author Change Date ******************************************************************************* - * Jan Källman Initial Release 2009-10-01 - * Jan Källman Total rewrite 2010-03-01 - * Jan Källman License changed GPL-->LGPL 2011-12-27 + * Jan Källman Initial Release 2009-10-01 + * Jan Källman Total rewrite 2010-03-01 + * Jan Källman License changed GPL-->LGPL 2011-12-27 * *******************************************************************************/ using System; using System.Xml; @@ -37,6 +37,7 @@ using System.Collections.Generic; using OfficeOpenXml.Drawing.Vml; using System.IO; +using OfficeOpenXml.Compatibility; using OfficeOpenXml.Drawing; using OfficeOpenXml.Utils; namespace OfficeOpenXml @@ -125,8 +126,12 @@ string id = ValidateImage(Alignment); //Add the image +#if (Core) + var img=ImageCompat.GetImageAsByteArray(Picture); +#else ImageConverter ic = new ImageConverter(); byte[] img = (byte[])ic.ConvertTo(Picture, typeof(byte[])); +#endif var ii = _ws.Workbook._package.AddImage(img); return AddImage(Picture, id, ii); @@ -154,10 +159,14 @@ throw (new InvalidDataException("File is not a supported image-file or is corrupt", ex)); } - ImageConverter ic = new ImageConverter(); string contentType = ExcelPicture.GetContentType(PictureFile.Extension); var uriPic = XmlHelper.GetNewUri(_ws._package.Package, "/xl/media/"+PictureFile.Name.Substring(0, PictureFile.Name.Length-PictureFile.Extension.Length) + "{0}" + PictureFile.Extension); +#if (Core) + var imgBytes=ImageCompat.GetImageAsByteArray(Picture); +#else + var ic = new ImageConverter(); byte[] imgBytes = (byte[])ic.ConvertTo(Picture, typeof(byte[])); +#endif var ii = _ws.Workbook._package.AddImage(imgBytes, uriPic, contentType); return AddImage(Picture, id, ii);