in src/Relecloud.Web.CallCenter.Api/Services/TicketManagementService/LocalTicketRenderingService.cs [48:100]
private MemoryStream RenderImage(Ticket ticket)
{
var ticketImageBlob = new MemoryStream();
if (ticket == null)
{
logger.LogWarning("Nothing to render for null ticket");
return ticketImageBlob;
}
if (ticket.Concert == null)
{
logger.LogWarning("Cannot find the concert related to this ticket");
return ticketImageBlob;
}
if (ticket.User == null)
{
logger.LogWarning("Cannot find the user related to this ticket");
return ticketImageBlob;
}
if (ticket.Customer == null)
{
logger.LogWarning("Cannot find the customer related to this ticket");
return ticketImageBlob;
}
using (var headerFont = new Font("Arial", 18, FontStyle.Bold))
using (var textFont = new Font("Arial", 12, FontStyle.Regular))
using (var bitmap = new Bitmap(640, 200, PixelFormat.Format24bppRgb))
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.White);
// Print concert details.
graphics.DrawString(ticket.Concert.Artist, headerFont, Brushes.DarkSlateBlue, new PointF(10, 10));
graphics.DrawString($"{ticket.Concert.Location} | {ticket.Concert.StartTime.UtcDateTime}", textFont, Brushes.Gray, new PointF(10, 40));
graphics.DrawString($"{ticket.Customer.Email} | {ticket.Concert.Price.ToString("c")}", textFont, Brushes.Gray, new PointF(10, 60));
// Print a fake barcode.
var random = new Random();
var offset = 15;
while (offset < 620)
{
var width = 2 * random.Next(1, 3);
graphics.FillRectangle(Brushes.Black, offset, 90, width, 90);
offset += width + (2 * random.Next(1, 3));
}
bitmap.Save(ticketImageBlob, ImageFormat.Png);
}
ticketImageBlob.Position = 0;
return ticketImageBlob;
}