Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions EPPlus/Drawing/ExcelPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Linq;
using OfficeOpenXml.Utils;
using OfficeOpenXml.Compatibility;

Expand All @@ -61,14 +62,15 @@ internal ExcelPicture(ExcelDrawings drawings, XmlNode node) :
Part = drawings.Part.Package.GetPart(UriPic);
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
//Re-traverse the stream to avoid Image.Save call which converts emf/wmf to png
var ms = Part.GetStream();
_image = Image.FromStream(ms);

var iby = new byte[ms.Length];
ms.Position = 0;
ms.Read(iby, 0, iby.Length);

var ii = _drawings._package.LoadImage(iby, UriPic, Part);
ImageHash = ii.Hash;

Expand Down Expand Up @@ -128,12 +130,10 @@ internal ExcelPicture(ExcelDrawings drawings, XmlNode node, FileInfo imageFile,
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
//Re-traverse the stream to avoid Image.Save call which converts emf/wmf to png
var img = new byte[imagestream.Length];
imagestream.Position = 0;
imagestream.Read(img, 0, img.Length);

imagestream.Close();
UriPic = GetNewUri(package, "/xl/media/{0}" + imageFile.Name);
Expand Down
86 changes: 84 additions & 2 deletions EPPlusTest/DrawingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using System.Diagnostics;
using System.Reflection;
using System.Drawing.Imaging;
using System.IO.Compression;

namespace EPPlusTest
{
Expand Down Expand Up @@ -977,5 +977,87 @@ public void DrawingWidthAdjust()
// p.SaveAs(new FileInfo(@"c:\temp\colwidthAdjust.xlsx"));
//}
}

#region ImageFormat Test

[TestMethod]
public void AddPicture_Bmp_StoresFormat()
{
AddPicture_Assert("BitmapImage.bmp", ImageFormat.Bmp);
}

[TestMethod]
public void AddPicture_Gif_StoresFormat()
{
AddPicture_Assert("BitmapImage.gif", ImageFormat.Gif);
}

[TestMethod]
public void AddPicture_Png_StoresFormat()
{
AddPicture_Assert("BitmapImage.png", ImageFormat.Png);
}

[TestMethod]
public void AddPicture_Tif_StoresFormat()
{
AddPicture_Assert("BitmapImage.tif", ImageFormat.Tiff);
}

[TestMethod]
public void AddPicture_Jpg_StoresFormat()
{
AddPicture_Assert("Test1.jpg", ImageFormat.Jpeg);
}

[TestMethod]
public void AddPicture_Emf_StoresFormat()
{
AddPicture_Assert("Vector Drawing.emf", ImageFormat.Emf);
}

[TestMethod]
public void AddPicture_Wmf_StoresFormat()
{
AddPicture_Assert("Vector Drawing.wmf", ImageFormat.Wmf);
}

public void AddPicture_Assert(string fileName, ImageFormat format)
{
using (var pck = new ExcelPackage())
{
var workbook = pck.Workbook;
var ws = workbook.Worksheets.Add("Sheet1");

var pic = ws.Drawings.AddPicture("Pic4", new FileInfo(Path.Combine(_clipartPath, fileName)));
pic.From.Row = 0;
pic.From.Column = 0;

pic.To.Row = 30;
pic.To.Column = 23;

using (var zip = new ZipArchive(new MemoryStream(pck.GetAsByteArray()), ZipArchiveMode.Read))
{
var found = false;

foreach (var entry in zip.Entries)
{
if (entry.Name != $"1{fileName}")
continue;

found = true;
var stream = entry.Open();
var drawing = Image.FromStream(stream);

Assert.AreEqual(format, drawing.RawFormat);
}

Assert.IsTrue(found, "Image was not found in zip.");
}
}

}

#endregion
}
}
5 changes: 5 additions & 0 deletions EPPlusTest/EPPlusTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Security" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase">
Expand Down Expand Up @@ -272,7 +273,11 @@
<EmbeddedResource Include="Resources\BitmapImage.gif" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\BitmapImage.bmp" />
<EmbeddedResource Include="Resources\BitmapImage.png" />
<EmbeddedResource Include="Resources\BitmapImage.tif" />
<Content Include="Resources\Test1.jpg" />
<EmbeddedResource Include="Resources\Vector Drawing.emf" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EPPlus\EPPlus.csproj">
Expand Down
Binary file added EPPlusTest/Resources/BitmapImage.bmp
Binary file not shown.
Binary file added EPPlusTest/Resources/BitmapImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added EPPlusTest/Resources/BitmapImage.tif
Binary file not shown.
Binary file added EPPlusTest/Resources/Vector Drawing.emf
Binary file not shown.
2 changes: 1 addition & 1 deletion EPPlusTest/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void InitBase()
var asm = Assembly.GetExecutingAssembly();
var validExtensions = new[]
{
".gif", ".wmf"
".gif", ".wmf", ".jpg", "emf", "png", "tif", "bmp"
};

foreach (var name in asm.GetManifestResourceNames())
Expand Down