简体   繁体   English

当我尝试使用WIA进行扫描时,为什么我的代码会引发异常?

[英]Why does my code throw an exception when I try to scan using WIA?

I'm making an application on C# Winforms which scans documents and places them into a PictureBox, However when I attempt to scan it throws an exception saying "Object Reference not set to an instance of an object" and will not allow me to continue, the stack trace is as below; 我正在C#Winforms上制作一个应用程序,该应用程序扫描文档并将其放入PictureBox,但是当我尝试扫描它时,抛出一个异常,指出“对象引用未设置为对象的实例”,并且不允许我继续,堆栈跟踪如下所示;

To clarify, this is a work project in case anyone gets alarmed by some of the class names. 要澄清的是,这是一个工作项目,以防有人对某些类名感到震惊。

AbDesktop.exe!AbDesktop.FrmCreditCards.ScanSetup() Line 39  C#
    AbDesktop.exe!AbDesktop.FrmCreditCards.ScanFrontBtn_Click(object sender, System.EventArgs e) Line 94 + 0x8 bytes    C#
    [External Code] 
    AbDesktop.exe!AbDesktop.Program.Main(string[] args) Line 26 + 0x20 bytes    C#
    [External Code] 

this is the code that is causing the issue; 这是导致问题的代码;

 public void ScanSetup()
 {
     WIA.CommonDialog dialog = new WIA.CommonDialog();
     ImageFile scannedImage=null;

     scannedImage = dialog.ShowAcquireImage(
         WiaDeviceType.ScannerDeviceType,
         WiaImageIntent.UnspecifiedIntent,
         WiaImageBias.MaximizeQuality,
         FormatID.wiaFormatPNG,
         true, true, false);
     scannedImage.SaveFile("C:/Users/reece.cottam/Pictures");
 }

and this is the button that executes the above code when the click event is fired 这是在触发click事件时执行上述代码的按钮

private void ScanFrontBtn_Click(object sender, EventArgs e)
{
    ScanSetup();
    ImageFile IF = new ImageFile();
    FrontScanBox.Image = IF.LoadFile("scannedimage.png");
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT The line of code causing the error is ScannedImage.Savefile 编辑导致错误的代码行是ScannedImage.Savefile

Without extra information about which line throws the exception, I would guess that it's the line scannedImage.SaveFile(...); 如果没有关于哪条线引发异常的额外信息,我想那是scannedImage.SaveFile(...); since the documentation for CommonDialog.ShowAcquireImage(...) states that a null value may be returned. 因为CommonDialog.ShowAcquireImage(...)文档指出可能返回空值。 In this case, when you try to operate on the variable scannedImage , you could be attempting to operate on a null reference. 在这种情况下,当您尝试对变量scannedImage进行操作时,可能会尝试对空引用进行操作。

The Documentation is pretty clear, the ShowAcquireImage method Returns an ImageFile object on success, otherwise returns Nothing. 文档非常清楚, ShowAcquireImage方法成功返回ImageFile对象,否则返回Nothing。 .

scannedImage is null because this method was not successful, you need to look at the parameters you are passing in here, read the documentation and adjust them appropriately until you are getting the image you want back. ScanningImage为null,因为此方法不成功,您需要查看此处传递的参数,阅读文档并进行适当调整,直到获得想要的图像为止。

Have you tried specifying an actual file, instead of just the directory to save to? 您是否尝试过指定实际文件,而不只是指定要保存的目录?

scannedImage.SaveFile("C:/Users/reece.cottam/Pictures/test.png");

The lack of a decent filename might cause errors in the SaveFile function. 缺少适当的文件名可能会导致SaveFile函数出错。 Though that should be visible in the Exception details. 虽然这应该在“异常”详细信息中可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试对DateTime进行排序时,为什么我的Linq查询会抛出异常? - Why does my Linq query throw an exception when I try and sort on DateTime? 为什么在应该表现为try / catch / finally的情况下使用using会引发异常? - Why does using throw an exception when it's supposed to behave like try/catch/finally? 为什么这段代码不会抛出异常? - Why does this code not throw an exception? 为什么我的代码会引发无效的强制转换异常? (C#)? - Why does my code throw an Invalid Cast Exception? (C#)? 为什么此代码会引发自动映射异常? - Why does this code throw an automapping exception? 如果我尝试在父类中使用TransactionScope,为什么EF&NUnit会引发异常? - Why does EF & NUnit throw an Exception if I try and use TransactionScope in a parent class? 为什么我的ServiceStack服务会抛出异常? - Why does my ServiceStack service throw an exception? 为什么我的自定义事件会引发异常? - Why does my custom event throw an exception? 使用WIA扫描多个文档 - Scan multiple documents using WIA 当我尝试将控制台输出重定向到文件时,为什么我的代码会写一个空白文件? - Why does my code write a blank file when I try to redirect Console output to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM