forked from kc3hack/2024_H
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
簡単な表紙画像の生成
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
KoeBook.Core/Contracts/Services/ICreateCoverFileService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |