diff --git a/KoeBook.Core/Contracts/Services/ICreateCoverFileService.cs b/KoeBook.Core/Contracts/Services/ICreateCoverFileService.cs new file mode 100644 index 0000000..1fc88fa --- /dev/null +++ b/KoeBook.Core/Contracts/Services/ICreateCoverFileService.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace KoeBook.Core.Contracts.Services; + +public interface ICreateCoverFileService +{ + /// + /// 表紙用の画像を作成 + /// + /// 作品の題名 + /// 作品の著者名 + /// 表紙の画像を置くフォルダのパス + /// 成功すれば、true、失敗すれば、false + void Create(string title, string author, string coverFilePath); +} diff --git a/KoeBook.Core/EbookException.cs b/KoeBook.Core/EbookException.cs index 497abfd..67a9ae3 100644 --- a/KoeBook.Core/EbookException.cs +++ b/KoeBook.Core/EbookException.cs @@ -71,4 +71,7 @@ public enum ExceptionType [EnumMember(Value = "不正なXMLです")] InvalidXml, + + [EnumMember(Value = "表紙の画像の生成に失敗しました")] + CreateCoverFileFailed, } diff --git a/KoeBook/Services/CreateCoverFileService.cs b/KoeBook/Services/CreateCoverFileService.cs new file mode 100644 index 0000000..6552e63 --- /dev/null +++ b/KoeBook/Services/CreateCoverFileService.cs @@ -0,0 +1,50 @@ +using System.Drawing; +using System.Drawing.Imaging; +using KoeBook.Core; +using KoeBook.Core.Contracts.Services; + +namespace KoeBook.Services; + +public class CreateCoverFileService : ICreateCoverFileService +{ + public void Create(string title, string author, string coverFilePath) + { + try + { + // ビットマップの作成 + // サイズはKindleガイドラインの推奨サイズによる + // https://kdp.amazon.co.jp/ja_JP/help/topic/G6GTK3T3NUHKLEFX + using var bitmap = new Bitmap(1600, 2560); + using var graphics = Graphics.FromImage(bitmap); + + // 塗りつぶし + graphics.FillRectangle(Brushes.PaleGoldenrod, graphics.VisibleClipBounds); + + // フォントの指定 + using var titleFont = new Font("游ゴシック Medium", 125, FontStyle.Bold); + using var authorFont = new Font("游ゴシック Medium", 75, FontStyle.Bold); + + // 色の指定 + var brush = Brushes.Black; + + // 表示位置の指定 + using var stringFormat = new StringFormat() + { + Alignment = StringAlignment.Center, + LineAlignment = StringAlignment.Center + }; + + // 文字の入力 + graphics.DrawString(title, titleFont, brush, new Rectangle(0, 0, 1600, 1920), stringFormat); + graphics.DrawString($"著者: {author}", authorFont, brush, new Rectangle(0, 1920, 1600, 640), stringFormat); + + // png として出力 + bitmap.Save(Path.Combine(coverFilePath, "Cover.png"), ImageFormat.Png); + } + catch (Exception ex) + { + throw new EbookException(ExceptionType.CreateCoverFileFailed, ex.Message, ex); + } + + } +}