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 4 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>
bool TryCreate(string title, string author, string coverFilePath);
}
47 changes: 47 additions & 0 deletions KoeBook/Services/CreateCoverFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Drawing;
using System.Drawing.Imaging;
using KoeBook.Core.Contracts.Services;

namespace KoeBook.Services;

public class CreateCoverFileService : ICreateCoverFileService
{
public bool TryCreate(string title, string author, string coverFilePath)
{
try
{
// ビットマップの作成
using Bitmap bitmap = new Bitmap(1800, 2560);
miyaji255 marked this conversation as resolved.
Show resolved Hide resolved
using Graphics graphics = Graphics.FromImage(bitmap);

// 塗りつぶし
graphics.FillRectangle(Brushes.PaleGoldenrod, graphics.VisibleClipBounds);

// フォントの指定
using Font titleFont = new Font("游ゴシック Medium", 125, FontStyle.Bold);
using Font authorFont = new Font("游ゴシック Medium", 75, FontStyle.Bold);
miyaji255 marked this conversation as resolved.
Show resolved Hide resolved

// 色の指定
using Brush brush = new SolidBrush(Color.Black);
miyaji255 marked this conversation as resolved.
Show resolved Hide resolved

// 表示位置の指定
using StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

// 文字の入力
graphics.DrawString(title, titleFont, brush, new Rectangle(0, 0, 1800, 1920), stringFormat);
graphics.DrawString($"著者: {author}", authorFont, brush, new Rectangle(0, 1920, 1800, 640), stringFormat);

// png として出力
bitmap.Save(Path.Combine(coverFilePath, "Cover.png"), ImageFormat.Png);

return true;
}
catch
{
return false;
}
miyaji255 marked this conversation as resolved.
Show resolved Hide resolved

}
}
Loading