Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

簡単な表紙画像の生成 #39

Merged
merged 15 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
}
Loading