Skip to content

Commit

Permalink
Merge pull request #39 from OUCC/feat/#28
Browse files Browse the repository at this point in the history
簡単な表紙画像の生成
  • Loading branch information
TakenPt authored May 1, 2024
2 parents 9ca7a6c + 2515351 commit c288d43
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions KoeBook.Core/Contracts/Services/ICreateCoverFileService.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 表紙用の画像を作成
/// </summary>
/// <param name="title">作品の題名</param>
/// <param name="author">作品の著者名</param>
/// <param name="coverFilePath">表紙の画像を置くフォルダのパス</param>
/// <returns>成功すれば、true、失敗すれば、false</returns>
void Create(string title, string author, string coverFilePath);
}
3 changes: 3 additions & 0 deletions KoeBook.Core/EbookException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ public enum ExceptionType

[EnumMember(Value = "不正なXMLです")]
InvalidXml,

[EnumMember(Value = "表紙の画像の生成に失敗しました")]
CreateCoverFileFailed,
}
50 changes: 50 additions & 0 deletions KoeBook/Services/CreateCoverFileService.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
}

0 comments on commit c288d43

Please sign in to comment.