xamarin - PdfSharpCore Perfomance Adding Image - Stack Overflow

I am building a .NET 9 MAUI application for Android. The app saves reports in SQLite and stores images

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;
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742327026a4422956.html

相关推荐

  • xamarin - PdfSharpCore Perfomance Adding Image - Stack Overflow

    I am building a .NET 9 MAUI application for Android. The app saves reports in SQLite and stores images

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信