From 17ad5125a1d1e720745209b884351197b5831c9f Mon Sep 17 00:00:00 2001 From: Arvydas Sidorenko Date: Wed, 28 Sep 2016 21:36:18 +0200 Subject: [PATCH] Removed dependency on System.Drawing. Bumped framework to netstandard1.3 in order to have System.IO.File available. --- EpubSharp.Tests/EpubWriterTests.cs | 2 +- EpubSharp/EpubBook.cs | 4 +-- EpubSharp/EpubReader.cs | 2 -- EpubSharp/EpubWriter.cs | 43 +++++++++++++++++++++--------- EpubSharp/project.json | 3 +-- 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/EpubSharp.Tests/EpubWriterTests.cs b/EpubSharp.Tests/EpubWriterTests.cs index 8eda06d..9f81677 100644 --- a/EpubSharp.Tests/EpubWriterTests.cs +++ b/EpubSharp.Tests/EpubWriterTests.cs @@ -98,7 +98,7 @@ public void AddRemoveTitleTest() public void SetCoverTest() { var writer = new EpubWriter(); - writer.SetCover(File.ReadAllBytes("Cover.png")); + writer.SetCover(File.ReadAllBytes("Cover.png"), ImageFormat.Png); var epub = WriteAndRead(writer); diff --git a/EpubSharp/EpubBook.cs b/EpubSharp/EpubBook.cs index 51d2eb7..ed6b504 100644 --- a/EpubSharp/EpubBook.cs +++ b/EpubSharp/EpubBook.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; +using System.Collections.Generic; using System.Linq; using System.Text; using EpubSharp.Format; diff --git a/EpubSharp/EpubReader.cs b/EpubSharp/EpubReader.cs index aed814e..0c423e9 100644 --- a/EpubSharp/EpubReader.cs +++ b/EpubSharp/EpubReader.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; -using System.Text; using System.Xml.Linq; using EpubSharp.Format; using EpubSharp.Format.Readers; diff --git a/EpubSharp/EpubWriter.cs b/EpubSharp/EpubWriter.cs index f681c57..beeeaf4 100644 --- a/EpubSharp/EpubWriter.cs +++ b/EpubSharp/EpubWriter.cs @@ -1,16 +1,18 @@ using System; -using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.IO.Compression; using System.Linq; -using System.Text; using System.Xml.Linq; using EpubSharp.Format; using EpubSharp.Format.Writers; namespace EpubSharp { + public enum ImageFormat + { + Gif, Png, Jpeg, Svg + } + public class EpubWriter { private readonly string opfPath = "EPUB/package.opf"; @@ -277,27 +279,42 @@ public void RemoveCover() } } - public void SetCover(byte[] data) + public void SetCover(byte[] data, ImageFormat imageFormat) { if (data == null) throw new ArgumentNullException(nameof(data)); RemoveCover(); - var image = Image.FromStream(new MemoryStream(data)); - if (image.RawFormat.Equals(ImageFormat.Png)) + string filename; + EpubContentType type; + + switch (imageFormat) { - using (var stream = new MemoryStream()) - { - image.Save(stream, ImageFormat.Png); - data = stream.ReadToEnd(); - } + case ImageFormat.Gif: + filename = "cover.gif"; + type = EpubContentType.ImageGif; + break; + case ImageFormat.Jpeg: + filename = "cover.jpeg"; + type = EpubContentType.ImageJpeg; + break; + case ImageFormat.Png: + filename = "cover.png"; + type = EpubContentType.ImagePng; + break; + case ImageFormat.Svg: + filename = "cover.svg"; + type = EpubContentType.ImageSvg; + break; + default: + throw new ArgumentException($"Unsupported cover format: {format}", nameof(format)); } var coverResource = new EpubByteFile { Content = data, - FileName = "cover.png", - ContentType = EpubContentType.ImagePng + FileName = filename, + ContentType = type }; coverResource.MimeType = ContentType.ContentTypeToMimeType[coverResource.ContentType]; resources.Images.Add(coverResource); diff --git a/EpubSharp/project.json b/EpubSharp/project.json index 69f934b..b5c163b 100644 --- a/EpubSharp/project.json +++ b/EpubSharp/project.json @@ -3,12 +3,11 @@ "dependencies": { "NETStandard.Library": "1.6.0", - "CoreCompat.System.Drawing": "1.0.0-beta006", "System.Xml.XmlDocument": "4.0.1" }, "frameworks": { - "netstandard1.2": { + "netstandard1.3": { "imports": "dnxcore50" } }