I am building a .NET 9 MAUI application for Android. The app saves reports in SQLite and stores images in the FileSystem.AppDataDirectory. This data is then used to generate a PDF file within the app. I using PdfSharpCore 1.3.65
I am encountering an issue where the app freezes when attempting to load an image using the following line of code:
var image = XImage.FromFile(photo.FilePath);
Below is the relevant part of the code where the problem occurs:
private void AddImages(PdfSharpCore.Pdf.PdfDocument pdfDocument, List<Photo> photos)
{
try
{
foreach (var photo in photos)
{
if (File.Exists(photo.FilePath))
{
PdfPage page = pdfDocument.AddPage();
XGraphics xGr = XGraphics.FromPdfPage(page);
var tmpPath = Path.Combine(FileSystem.CacheDirectory, photo. Filename);
// At this line the app is frozen.
var image = XImage.FromFile(photo.FilePath);
xGr.DrawImage(image, 10, 10, image.PixelWidth, image.PixelHeight);
}
}
}
catch (Exception e)
{
throw;
}
}
public async Task<byte[]> EntryExitReport(EntryExitReport i)
{
try
{
using (PdfDocument document = new PdfDocument())
{
PdfPage page = document.AddPage();
GenerateTableEntry(page, i);
if(i.Photos?.Count> 0)
{
AddImages(document, i.Photos.ToList());
}
// Create a MemoryStream for save the byte array
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream);
return stream.ToArray();
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error al generar el reporte: {ex.Message}");
return null;
}
}