c# - 扫描后在 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);
}
}
解决方案
正如我在评论中所写,它看起来像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 图像
推荐阅读
- javascript - Javascript 网站安全 - 全局变量
- ios - 从断开连接的蓝牙设备捕获输入时,AVAudioSession 服务重置
- r - 创建一个优化模型,以便选择开始日期和结束日期会产生一些结果
- c - 未初始化条件 Valgrind
- python - WIndows - Python 字符串到时间
- delphi - Delphi Rio 打补丁后无法启动
- typescript - 使用泛型类型时打字稿类型缩小不起作用
- javascript - paypal API v2如何在交易完成后重新加载
- python - 删除 2dlists 字典中的相似值
- javascript - 简单的 localStorage 刷新代码不起作用