NullReferenceException WIA C#
当我运行下面的代码时,出现此错误
NullreferenceException was unhandled..
下面是我的代码,
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
谢谢
// 完整的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string scannerName;
private Device scanner;
private Item imageItem;
private const int ADF = 1;
private const int FLATBED = 2;
private const int DEVICE_NAME_PROPERTY_ID = 7;
private const int DOCUMENT_HANDLING_PROPERTY_ID = 3088;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
showScannerDialog();
}
public static string[] GetScannerList()
{
ArrayList scannerList = new ArrayList();
DeviceManager deviceManager = new DeviceManager();
if (deviceManager.DeviceInfos.Count == 0)
{
return new string[0]; // return an empty string array
}
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
scannerList.Add(getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID));
}
}
return (string[])scannerList.ToArray(typeof(string));
}
public int Init(string scannerName)
{
this.scannerName = scannerName;
this.showScannerDialog();
if (this.imageItem == null)
{
return 0;
}
else
{
return 1;
}
}
public int Scan(string filePath)
{
Tiff tiff = new Tiff(filePath);
bool adf = false;
int numScans = 0;
ArrayList tempFiles = new ArrayList();
// determine if the scanner is set to use an ADF
string docHandlingSelect = getDeviceProperty(this.scanner, DOCUMENT_HANDLING_PROPERTY_ID);
if (docHandlingSelect != "")
{
try
{
if ((int.Parse(docHandlingSelect) & ADF) == ADF)
{
adf = true;
}
}
catch { }
}
while (true)
{
string tempFile = Path.GetTempFileName();
tempFiles.Add(tempFile);
File.Delete(tempFile);
ImageFile wiaFile = (ImageFile)imageItem.Transfer(FormatID.wiaFormatTIFF);
wiaFile.SaveFile(tempFile);
Image tempImage = Image.FromFile(tempFile);
tiff.AddImage(tempImage);
tempImage.Dispose();
numScans++;
if (!adf)
{
DialogResult result =
MessageBox.Show("Do you wish to scan another page and save it to the same file?",
"Scanner Message", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
break;
}
this.showScannerDialog();
if (this.imageItem == null)
{
break;
}
}
}
tiff.Close();
foreach (string f in tempFiles.ToArray(typeof(string)))
{
File.Delete(f);
}
Marshal.ReleaseComObject(imageItem);
if (numScans == 0)
{
throw new Exception("Nothing was scanned.");
}
return 1;
}
private void getScanner()
{
DeviceManager deviceManager = new DeviceManager();
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
if (this.scannerName == getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID))
{
this.scanner = device;
}
}
}
}
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
private static string getDeviceProperty(Device device, int propteryID)
{
string retVal = "";
foreach (Property prop in device.Properties)
{
if (prop.PropertyID == propteryID)
{
retVal = prop.get_Value().ToString();
break;
}
}
return retVal;
}
}
}
When I run the code below I get this error
NullreferenceException was unhandled..
below is my code
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
thanks
// complete code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string scannerName;
private Device scanner;
private Item imageItem;
private const int ADF = 1;
private const int FLATBED = 2;
private const int DEVICE_NAME_PROPERTY_ID = 7;
private const int DOCUMENT_HANDLING_PROPERTY_ID = 3088;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
showScannerDialog();
}
public static string[] GetScannerList()
{
ArrayList scannerList = new ArrayList();
DeviceManager deviceManager = new DeviceManager();
if (deviceManager.DeviceInfos.Count == 0)
{
return new string[0]; // return an empty string array
}
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
scannerList.Add(getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID));
}
}
return (string[])scannerList.ToArray(typeof(string));
}
public int Init(string scannerName)
{
this.scannerName = scannerName;
this.showScannerDialog();
if (this.imageItem == null)
{
return 0;
}
else
{
return 1;
}
}
public int Scan(string filePath)
{
Tiff tiff = new Tiff(filePath);
bool adf = false;
int numScans = 0;
ArrayList tempFiles = new ArrayList();
// determine if the scanner is set to use an ADF
string docHandlingSelect = getDeviceProperty(this.scanner, DOCUMENT_HANDLING_PROPERTY_ID);
if (docHandlingSelect != "")
{
try
{
if ((int.Parse(docHandlingSelect) & ADF) == ADF)
{
adf = true;
}
}
catch { }
}
while (true)
{
string tempFile = Path.GetTempFileName();
tempFiles.Add(tempFile);
File.Delete(tempFile);
ImageFile wiaFile = (ImageFile)imageItem.Transfer(FormatID.wiaFormatTIFF);
wiaFile.SaveFile(tempFile);
Image tempImage = Image.FromFile(tempFile);
tiff.AddImage(tempImage);
tempImage.Dispose();
numScans++;
if (!adf)
{
DialogResult result =
MessageBox.Show("Do you wish to scan another page and save it to the same file?",
"Scanner Message", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
break;
}
this.showScannerDialog();
if (this.imageItem == null)
{
break;
}
}
}
tiff.Close();
foreach (string f in tempFiles.ToArray(typeof(string)))
{
File.Delete(f);
}
Marshal.ReleaseComObject(imageItem);
if (numScans == 0)
{
throw new Exception("Nothing was scanned.");
}
return 1;
}
private void getScanner()
{
DeviceManager deviceManager = new DeviceManager();
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
if (this.scannerName == getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID))
{
this.scanner = device;
}
}
}
}
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
private static string getDeviceProperty(Device device, int propteryID)
{
string retVal = "";
foreach (Property prop in device.Properties)
{
if (prop.PropertyID == propteryID)
{
retVal = prop.get_Value().ToString();
break;
}
}
return retVal;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将 this.scanner 传递给dialog.ShowSelectItems() 时,它被设置为null。该函数可能会尝试使用它而不检查它是否为空。
this.scanner is set to null when you pass it to dialog.ShowSelectItems(). The function may be trying to use it without checking if it is null.