2023-11-05 22:20:35 +00:00
|
|
|
using System;
|
2019-09-29 16:47:06 +00:00
|
|
|
using System.Drawing;
|
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
namespace PKHeX.Drawing.Misc;
|
|
|
|
|
|
|
|
public static class QRImageUtil
|
2019-09-29 16:47:06 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
public static Bitmap GetQRImage(Image qr, Image preview)
|
2019-09-29 16:47:06 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
// create a small area with the pk sprite, with a white background
|
2021-12-10 08:15:04 +00:00
|
|
|
var foreground = new Bitmap(preview.Width + 4, preview.Height + 4);
|
|
|
|
using (Graphics gfx = Graphics.FromImage(foreground))
|
2019-09-29 16:47:06 +00:00
|
|
|
{
|
2023-11-05 22:20:35 +00:00
|
|
|
gfx.FillRectangle(Brushes.White, 0, 0, foreground.Width, foreground.Height);
|
2021-12-10 08:15:04 +00:00
|
|
|
int x = (foreground.Width / 2) - (preview.Width / 2);
|
|
|
|
int y = (foreground.Height / 2) - (preview.Height / 2);
|
|
|
|
gfx.DrawImage(preview, x, y);
|
2019-09-29 16:47:06 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 08:15:04 +00:00
|
|
|
// Layer on Preview Image
|
2019-09-29 16:47:06 +00:00
|
|
|
{
|
2021-12-10 08:15:04 +00:00
|
|
|
int x = (qr.Width / 2) - (foreground.Width / 2);
|
|
|
|
int y = (qr.Height / 2) - (foreground.Height / 2);
|
|
|
|
return ImageUtil.LayerImage(qr, foreground, x, y);
|
2019-09-29 16:47:06 +00:00
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
}
|
2019-09-29 16:47:06 +00:00
|
|
|
|
2023-11-05 22:20:35 +00:00
|
|
|
public static Bitmap GetQRImageExtended(Font font, Image qr, Image pk, int width, int height, ReadOnlySpan<string> lines, string extraText)
|
2021-12-10 08:15:04 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
var pic = GetQRImage(qr, pk);
|
2021-12-10 08:15:04 +00:00
|
|
|
return ExtendImage(font, qr, width, height, pic, lines, extraText);
|
|
|
|
}
|
2019-09-29 16:47:06 +00:00
|
|
|
|
2023-11-05 22:20:35 +00:00
|
|
|
private static Bitmap ExtendImage(Font font, Image qr, int width, int height, Image pic, ReadOnlySpan<string> lines, string extraText)
|
2021-12-10 08:15:04 +00:00
|
|
|
{
|
|
|
|
var newpic = new Bitmap(width, height);
|
|
|
|
using Graphics g = Graphics.FromImage(newpic);
|
2023-11-05 22:20:35 +00:00
|
|
|
g.FillRectangle(Brushes.White, 0, 0, newpic.Width, newpic.Height);
|
2021-12-10 08:15:04 +00:00
|
|
|
g.DrawImage(pic, 0, 0);
|
2019-09-29 16:47:06 +00:00
|
|
|
|
2023-12-09 23:21:10 +00:00
|
|
|
var black = Brushes.Black;
|
|
|
|
const int indent = 18;
|
2023-12-11 04:58:58 +00:00
|
|
|
g.DrawString(GetLine(lines, 0), font, black, indent, qr.Height - 5);
|
|
|
|
g.DrawString(GetLine(lines, 1), font, black, indent, qr.Height + 8);
|
|
|
|
g.DrawString(GetLine2(lines) , font, black, indent, qr.Height + 20);
|
|
|
|
g.DrawString(GetLine(lines, 3) + extraText, font, black, indent, qr.Height + 32);
|
2021-12-10 08:15:04 +00:00
|
|
|
return newpic;
|
2019-09-29 16:47:06 +00:00
|
|
|
}
|
2021-12-10 08:15:04 +00:00
|
|
|
|
2023-12-09 23:21:10 +00:00
|
|
|
private static string GetLine2(ReadOnlySpan<string> lines) => GetLine(lines, 2)
|
|
|
|
.Replace(Environment.NewLine, "/")
|
|
|
|
.Replace("//", " ")
|
|
|
|
.Replace(":/", ": ");
|
|
|
|
|
2023-11-05 22:20:35 +00:00
|
|
|
private static string GetLine(ReadOnlySpan<string> lines, int line) => lines.Length <= line ? string.Empty : lines[line];
|
2019-09-29 16:47:06 +00:00
|
|
|
}
|