最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xamarin - PdfSharpCore Perfomance Adding Image - Stack Overflow

programmeradmin2浏览0评论

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;
        }
    }
发布评论

评论列表(0)

  1. 暂无评论