我想在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);
}
}
正如我在评论中所写看起来foreach
循环覆盖了所有图像,为什么你似乎得到了最后一张图片,你应该使用类似page.GetImages().FirstOrDefault()
我的意思是,您遍历PDF中的所有页面和页面中的所有图像,并将每个图像放在相同的pictureBox
如何使用 FirstOrDefault:
这将获取可为 null 的整数列表,这意味着该项可以是 int 或 null
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 图像