Skip to content

Commit

Permalink
Removed dependency on System.Drawing. Bumped framework to netstandard…
Browse files Browse the repository at this point in the history
…1.3 in order to have System.IO.File available.
  • Loading branch information
Arvydas Sidorenko committed Sep 28, 2016
1 parent 50ca450 commit 17ad512
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion EpubSharp.Tests/EpubWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions EpubSharp/EpubBook.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 0 additions & 2 deletions EpubSharp/EpubReader.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
43 changes: 30 additions & 13 deletions EpubSharp/EpubWriter.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions EpubSharp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down

0 comments on commit 17ad512

Please sign in to comment.