首页 > 解决方案 > 扫描后在 UI 中加载第一页

问题描述

我想在 UI 中加载扫描的第一页(在附加的扫描仪中),但现在在这段代码中,最后一页加载到 UI 中。任何人都可以分析代码并告诉我究竟要重组什么以显示第一页吗? 在此处输入图像描述

 /// <summary>
    /// Loads the invoice in the UI, with its associated data.
    /// </summary>
    private void ShowImFromPDF()
    {
        IoC.Main.InvoiceCount = IoC.Main.Invoices.Count;

        GlobalVars.WriteLog("Updating image and data");
        if (IoC.Main.InvoiceIndex >= 0 && IoC.Main.InvoiceIndex < IoC.Main.Invoices.Count)
        {
            IoC.Main.LoadInfo = true;
            PdfDocument document = PdfReader.Open(IoC.Main.Invoices[IoC.Main.InvoiceIndex].Path);

            foreach (PdfPage page in document.Pages)
            {
                foreach (System.Drawing.Image image in page.GetImages())
                {
                    pictureBox1.Source = HelperMethods.ToBitMapImage(image);
                }
            }

标签: c#xmlwpfvisual-studioc#-4.0

解决方案


正如我在评论中所写,它看起来像foreach循环覆盖所有图像,为什么你似乎得到最后一张图片,你应该使用类似page.GetImages().FirstOrDefault() 我的意思的东西,你遍历 pdf 中的所有页面和页面内的所有图像并放置每个对同样pictureBox

如何使用 FirstOrDefault:

这得到一个可以为空的整数列表,这意味着该项目可以是整数或空

 public static void doStuff(List<int?> nullableList)
        {            

            var firstItem = nullableList.FirstOrDefault();
            if (firstItem != null)
                Console.WriteLine(firstItem);
            else
                Console.WriteLine("first item is null");
        }

发送示例

   List<int?> nullableList = new List<int?>() { 1, null, 2, 3, null };
   doStuff(nullableList);

    List<int?> nullableList1 = new List<int?>() { null, null, 2, 3, null };
    doStuff(nullableList1);

结果

1

“第一项为空”

取决于您的逻辑,您应该从 y 页面获取 x 图像


推荐阅读