Skip to content

Commit

Permalink
fix: adapt to breaking changes in SixLabors.ImageSharp
Browse files Browse the repository at this point in the history
  • Loading branch information
hueifeng committed Jan 10, 2024
1 parent f055e05 commit 129ab70
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 37 deletions.
9 changes: 7 additions & 2 deletions src/EPPlus/EPPlus/Drawing/ExcelPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Jpeg;
using Magicodes.IE.EPPlus;

namespace OfficeOpenXml.Drawing
{
Expand All @@ -59,7 +60,9 @@ internal ExcelPicture(ExcelDrawings drawings, XmlNode node) :
UriPic = UriHelper.ResolvePartUri(drawings.UriDrawing, RelPic.TargetUri);

Part = drawings.Part.Package.GetPart(UriPic);
Image = Image.Load(Part.GetStream(), out var format);
// Image = Image.Load(Part.GetStream(), out var format);
Image = Image.Load(Part.GetStream());
var format = Image.GetImageFormat(Part.GetStream());
ImageFormat = format;
byte[] iby = ImageCompat.GetImageAsByteArray(Image, format);
var ii = _drawings._package.LoadImage(iby, UriPic, Part);
Expand Down Expand Up @@ -119,7 +122,9 @@ internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile,
var package = drawings.Worksheet._package.Package;
using (var imageStream = new FileStream(imageFile.FullName, FileMode.Open, FileAccess.Read))
{
Image = Image.Load(imageStream, out var format);
// Image = Image.Load(imageStream, out var format);
Image = Image.Load(imageStream);
var format = Image.GetImageFormat(imageStream);
ImageFormat = format;

var img = ImageCompat.GetImageAsByteArray(Image, format);
Expand Down
14 changes: 11 additions & 3 deletions src/EPPlus/EPPlus/ExcelBackgroundImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
using System.Xml;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using Magicodes.IE.EPPlus;

namespace OfficeOpenXml
{
Expand Down Expand Up @@ -77,9 +78,14 @@ public void SetImage(byte[] imageBytes = null)
DeleteAllNode(BACKGROUNDPIC_PATH);
return;
}
Image = Image.Load(imageBytes);
//Image = Image.Load(imageBytes, out var imageFormat);
using (MemoryStream imageStream = new MemoryStream(imageBytes))
{
var imageFormat = Image.GetImageFormat(imageStream);
ImageFormat = imageFormat;
}

Image = Image.Load(imageBytes, out var imageFormat);
ImageFormat = imageFormat;
var imageInfo = _workSheet.Workbook._package.AddImage(imageBytes);
var rel = _workSheet.Part.CreateRelationship(imageInfo.Uri, Packaging.TargetMode.Internal,
ExcelPackage.schemaRelationships + "/image");
Expand All @@ -98,7 +104,9 @@ public void SetFromFile(FileInfo pictureFile)
try
{
var fileBytes = File.ReadAllBytes(pictureFile.FullName);
Image.Load(fileBytes, out var format);
//Image.Load(fileBytes, out var format);
var format = Image.DetectFormat(fileBytes);

string contentType = format.DefaultMimeType;
var imageUri = XmlHelper.GetNewUri(_workSheet._package.Package,
"/xl/media/" +
Expand Down
21 changes: 15 additions & 6 deletions src/EPPlus/EPPlus/ExcelHeaderFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
using System.Xml;
using SixLabors.ImageSharp;

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace

Check warning on line 41 in src/EPPlus/EPPlus/ExcelHeaderFooter.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The using directive for 'SixLabors.ImageSharp' appeared previously in this namespace
using SixLabors.ImageSharp.Formats;
using System.Reflection;
using Magicodes.IE.EPPlus;

namespace OfficeOpenXml
{
Expand Down Expand Up @@ -150,15 +152,22 @@ public ExcelVmlDrawingPicture InsertPicture(FileInfo pictureFile, PictureAlignme
{
throw (new FileNotFoundException(string.Format("{0} is missing", pictureFile.FullName)));
}
Picture = Image.Load(pictureFile.FullName, out var format);
string contentType = format.DefaultMimeType;
var uriPic = XmlHelper.GetNewUri(_ws._package.Package, "/xl/media/" + pictureFile.Name.Substring(0, pictureFile.Name.Length - pictureFile.Extension.Length) + "{0}" + pictureFile.Extension);
var imgBytes = ImageCompat.GetImageAsByteArray(Picture, format);

// Picture = Image.Load(pictureFile.FullName, out var format);
Picture = Image.Load(pictureFile.FullName);
using (Stream imageStream = pictureFile.OpenRead())
{
var format = Picture.GetImageFormat(imageStream);
string contentType = format.DefaultMimeType;
var uriPic = XmlHelper.GetNewUri(_ws._package.Package, "/xl/media/" + pictureFile.Name.Substring(0, pictureFile.Name.Length - pictureFile.Extension.Length) + "{0}" + pictureFile.Extension);
var imgBytes = ImageCompat.GetImageAsByteArray(Picture, format);


var ii = _ws.Workbook._package.AddImage(imgBytes, uriPic, contentType);
var ii = _ws.Workbook._package.AddImage(imgBytes, uriPic, contentType);

return AddImage(Picture, format, id, ii);
return AddImage(Picture, format, id, ii);
}

}
catch (Exception ex)
{
Expand Down
6 changes: 3 additions & 3 deletions src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="6.0.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="7.0.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="System.Security.Cryptography.Pkcs" Version="8.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>
Expand Down
49 changes: 49 additions & 0 deletions src/EPPlus/EPPlus/Utils/ImageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Formats;

namespace Magicodes.IE.EPPlus
{
public static partial class ImageExtensions
{
public static IImageFormat GetImageFormat(this Image image, Stream imageStream)
{
var metadataProperty = image.Metadata.GetType().GetProperty("DecodedImageFormat");
if (metadataProperty != null)
{
return metadataProperty.GetValue(image.Metadata) as IImageFormat;
}
else
{
byte[] header = new byte[4];
long originalPosition = imageStream.Position;

imageStream.Position = 0;
imageStream.Read(header, 0, header.Length);
imageStream.Position = originalPosition;

if (header[0] == 0x89 && header[1] == 0x50 && header[2] == 0x4E && header[3] == 0x47)
{
return PngFormat.Instance;
}
if (header[0] == 0xFF && header[1] == 0xD8)
{
return JpegFormat.Instance;
}
if (header[0] == 0x47 && header[1] == 0x49 && header[2] == 0x46)
{
return GifFormat.Instance;
}
if (header[0] == 0x42 && header[1] == 0x4D)
{
return BmpFormat.Instance;
}
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.0" />
</ItemGroup>
Expand Down
59 changes: 42 additions & 17 deletions src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Metadata;
using SkiaSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using Magicodes.IE.EPPlus;
using SixLabors.ImageSharp.Memory;
using System.Text.RegularExpressions;

namespace Magicodes.IE.Excel.Images
{
internal static class ImageExtensions
internal static partial class ImageExtensions
{
public static string SaveTo(this Image image, string path)
{
Expand Down Expand Up @@ -39,34 +46,52 @@ public static Image GetImageByUrl(this string url, out IImageFormat format)
using (var wc = new WebClient())

Check warning on line 46 in src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 46 in src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 46 in src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 46 in src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 46 in src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
{
wc.Proxy = null;
var image = Image.Load(wc.OpenRead(url), out format);
if (image.Metadata.HorizontalResolution == 0 && image.Metadata.VerticalResolution == 0)
using (Stream webStream = wc.OpenRead(url))
{
image.Metadata.HorizontalResolution = ImageMetadata.DefaultHorizontalResolution;
image.Metadata.VerticalResolution = ImageMetadata.DefaultVerticalResolution;
using (MemoryStream memoryStream = new MemoryStream())
{
webStream.CopyTo(memoryStream);
memoryStream.Position = 0;

var image = Image.Load(memoryStream);
format = image.GetImageFormat(memoryStream);

if (image.Metadata.HorizontalResolution == 0 && image.Metadata.VerticalResolution == 0)
{
image.Metadata.HorizontalResolution = ImageMetadata.DefaultHorizontalResolution;
image.Metadata.VerticalResolution = ImageMetadata.DefaultVerticalResolution;
}
return image;
}
}
return image;
}
}

/// <summary>
/// base64转Bitmap
/// Converts a base64 string to an Image
/// </summary>
/// <param name="base64String"></param>
/// <param name="format"></param>
/// <returns></returns>
/// <param name="base64String">The base64 string representing the image</param>
/// <param name="format">The image format</param>
/// <returns>An Image object representing the base64 string</returns>
public static Image Base64StringToImage(this string base64String, out IImageFormat format)
{
var bytes = Convert.FromBase64String(FixBase64ForImage(base64String));
return Image.Load(bytes, out format);
byte[] bytes = Convert.FromBase64String(CleanupBase64String(base64String));
using (MemoryStream stream = new MemoryStream(bytes))
{
Image image = Image.Load(stream);
format = image.GetImageFormat(stream);
return image;
}
}

private static string FixBase64ForImage(string image)
/// <summary>
/// Cleans up the base64 string by removing unnecessary characters
/// </summary>
/// <param name="base64String">The base64 string to clean up</param>
/// <returns>A cleaned-up base64 string</returns>
private static string CleanupBase64String(string base64String)
{
var sbText = new System.Text.StringBuilder(image, image.Length);
sbText.Replace("\r\n", string.Empty);
sbText.Replace(" ", string.Empty);
return sbText.ToString();
return Regex.Replace(base64String, @"\s+", string.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ protected virtual void ParseTemplate(ExcelPackage excelPackage)
/// 解析头部
/// </summary>
/// <returns></returns>
/// <exception cref = "ArgumentException" > 导入实体没有定义ImporterHeader属性 </ exception >
/// <exception cref = "ArgumentException" > 导入实体没有定义ImporterHeader属性
/// </exception>
protected virtual bool ParseHeader()
{
ImporterHeaderInfos = new List<ImporterHeaderInfo>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
using Magicodes.IE.Excel.Images;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SkiaSharp;
using Magicodes.IE.EPPlus;
using System.Reflection;

namespace Magicodes.ExporterAndImporter.Excel.Utility.TemplateExport
{
Expand Down Expand Up @@ -696,7 +699,13 @@ private bool RenderCellPipeline(Interpreter target, ExcelWorksheet sheet, ref st
}
else if (File.Exists(imageUrl))
{
image = Image.Load(imageUrl, out format);
using (Stream imageStream = File.OpenRead(imageUrl))
{
image = Image.Load(imageStream);
format = image.GetImageFormat(imageStream);
}

// image = Image.Load(imageUrl, out format);
}

if (image == null)
Expand All @@ -719,7 +728,7 @@ private bool RenderCellPipeline(Interpreter target, ExcelWorksheet sheet, ref st
excelImage.SetSize(width, height);
}
}
catch (Exception)
catch (Exception ex)

Check warning on line 731 in src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateExportHelper.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The variable 'ex' is declared but never used

Check warning on line 731 in src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateExportHelper.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The variable 'ex' is declared but never used

Check warning on line 731 in src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateExportHelper.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The variable 'ex' is declared but never used

Check warning on line 731 in src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateExportHelper.cs

View workflow job for this annotation

GitHub Actions / macOS-latest

The variable 'ex' is declared but never used
{
cell.Value = alt;
}
Expand Down

0 comments on commit 129ab70

Please sign in to comment.