diff --git a/EPPlus/Drawing/ExcelPicture.cs b/EPPlus/Drawing/ExcelPicture.cs index e413ae025..d3a0ee41d 100644 --- a/EPPlus/Drawing/ExcelPicture.cs +++ b/EPPlus/Drawing/ExcelPicture.cs @@ -38,6 +38,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.Diagnostics; +using System.Linq; using OfficeOpenXml.Utils; using OfficeOpenXml.Compatibility; @@ -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; @@ -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); diff --git a/EPPlusTest/DrawingTest.cs b/EPPlusTest/DrawingTest.cs index f027c444e..3e715c1b3 100644 --- a/EPPlusTest/DrawingTest.cs +++ b/EPPlusTest/DrawingTest.cs @@ -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 { @@ -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 } } diff --git a/EPPlusTest/EPPlusTest.csproj b/EPPlusTest/EPPlusTest.csproj index 6248deafe..98d67a88a 100644 --- a/EPPlusTest/EPPlusTest.csproj +++ b/EPPlusTest/EPPlusTest.csproj @@ -85,6 +85,7 @@ + @@ -272,7 +273,11 @@ + + + + diff --git a/EPPlusTest/Resources/BitmapImage.bmp b/EPPlusTest/Resources/BitmapImage.bmp new file mode 100644 index 000000000..b6e8129ce Binary files /dev/null and b/EPPlusTest/Resources/BitmapImage.bmp differ diff --git a/EPPlusTest/Resources/BitmapImage.png b/EPPlusTest/Resources/BitmapImage.png new file mode 100644 index 000000000..248456639 Binary files /dev/null and b/EPPlusTest/Resources/BitmapImage.png differ diff --git a/EPPlusTest/Resources/BitmapImage.tif b/EPPlusTest/Resources/BitmapImage.tif new file mode 100644 index 000000000..dec9713a6 Binary files /dev/null and b/EPPlusTest/Resources/BitmapImage.tif differ diff --git a/EPPlusTest/Resources/Vector Drawing.emf b/EPPlusTest/Resources/Vector Drawing.emf new file mode 100644 index 000000000..5374da80a Binary files /dev/null and b/EPPlusTest/Resources/Vector Drawing.emf differ diff --git a/EPPlusTest/TestBase.cs b/EPPlusTest/TestBase.cs index 3e062cfbb..428b3fc98 100644 --- a/EPPlusTest/TestBase.cs +++ b/EPPlusTest/TestBase.cs @@ -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())