的NullReferenceException WIA C# [英] NullReferenceException WIA C#
本文介绍了的NullReferenceException WIA C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行code下面我得到这个错误
的NullReferenceException是未处理的。
下面是我的code
私人无效showScannerDialog()
{
this.scanner = NULL;
this.imageItem = NULL;
this.getScanner();
WIA.CommonDialog对话框=新WIA.CommonDialog();
项目imageItems = dialog.ShowSelectItems(this.scanner,WiaImageIntent.TextIntent,WiaImageBias.MinimizeSize,假的,真的,假的);
如果(imageItems!= NULL)
{
的foreach(在imageItems项项)
{
的ImageItem =项目;
打破;
}
}
}
感谢
//完整的code
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Windows.Forms的;
使用System.IO;
System.Collections中使用;
使用System.Drawing.Imaging;
使用了System.Runtime.InteropServices;
命名空间WindowsFormsApplication1
{
公共部分类Form1中:形态
{
串scannerName;
私有设备扫描仪;
私人物品的ImageItem;
私人const int的ADF = 1;
私人const int的平板式= 2;
私人const int的DEVICE_NAME_PROPERTY_ID = 7;
私人const int的DOCUMENT_HANDLING_PROPERTY_ID = 3088;
公共Form1中()
{
的InitializeComponent();
}
私人无效Form1_Load的(对象发件人,EventArgs的)
{
showScannerDialog();
}
公共静态字符串[] GetScannerList()
{
ArrayList的scannerList =新的ArrayList();
DeviceManager deviceManager =新DeviceManager();
如果(deviceManager.DeviceInfos.Count == 0)
{
返回新的字符串[0]; //返回一个空字符串数组
}
的foreach(DeviceInfo deviceInfo在deviceManager.DeviceInfos)
{
如果(deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
设备device = deviceInfo.Connect();
scannerList.Add(getDeviceProperty(设备,DEVICE_NAME_PROPERTY_ID));
}
}
返程(字符串[])scannerList.ToArray(typeof运算(字符串));
}
公众诠释初始化(字符串scannerName)
{
this.scannerName = scannerName;
this.showScannerDialog();
如果(this.imageItem == NULL)
{
返回0;
}
其他
{
返回1;
}
}
公众诠释扫描(字符串文件路径)
{
TIFF TIFF =新的TIFF(文件路径);
布尔ADF = FALSE;
INT numScans = 0;
ArrayList的临时文件=新的ArrayList();
//确定扫描仪设置为使用ADF
字符串docHandlingSelect = getDeviceProperty(this.scanner,DOCUMENT_HANDLING_PROPERTY_ID);
如果(docHandlingSelect!=)
{
尝试
{
如果((int.Parse(docHandlingSelect)及ADF)== ADF)
{
ADF = TRUE;
}
}
抓住 { }
}
而(真)
{
字符串TEMPFILE = Path.GetTempFileName();
tempFiles.Add(临时文件);
File.Delete(临时文件);
镜像文件wiaFile =(镜像文件)imageItem.Transfer(FormatID.wiaFormatTIFF);
wiaFile.SaveFile(临时文件);
图片tempImage = Image.FromFile(临时文件);
tiff.AddImage(tempImage);
tempImage.Dispose();
numScans ++;
如果(!ADF)
{
DialogResult的结果=
的MessageBox.show(你要扫描另一页,并将其保存到同一个文件?,
扫描仪信息,MessageBoxButtons.YesNo);
如果(结果== DialogResult.No)
{
打破;
}
this.showScannerDialog();
如果(this.imageItem == NULL)
{
打破;
}
}
}
tiff.Close();
的foreach(在tempFiles.ToArray串F(typeof运算(字符串)))
{
File.Delete(F);
}
对Marshal.ReleaseComObject(ImageItem的);
如果(numScans == 0)
{
抛出新的异常(没有被扫描。);
}
返回1;
}
私人无效getScanner()
{
DeviceManager deviceManager =新DeviceManager();
的foreach(DeviceInfo deviceInfo在deviceManager.DeviceInfos)
{
如果(deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
设备device = deviceInfo.Connect();
如果(this.scannerName == getDeviceProperty(设备,DEVICE_NAME_PROPERTY_ID))
{
this.scanner =设备;
}
}
}
}
私人无效showScannerDialog()
{
this.scanner = NULL;
this.imageItem = NULL;
this.getScanner();
WIA.CommonDialog对话框=新WIA.CommonDialog();
项目imageItems = dialog.ShowSelectItems(this.scanner,WiaImageIntent.TextIntent,WiaImageBias.MinimizeSize,假的,真的,假的);
如果(imageItems!= NULL)
{
的foreach(在imageItems项项)
{
的ImageItem =项目;
打破;
}
}
}
私人静态字符串getDeviceProperty(设备装置,诠释propteryID)
{
字符串retVal的=;
的foreach(在device.Properties属性道具)
{
如果(prop.PropertyID == propteryID)
{
。retVal的= prop.get_Value()的ToString();
打破;
}
}
返回retVal的;
}
}
}
解决方案
this.scanner设置为空当你将它传递给dialog.ShowSelectItems()。该功能可尝试使用它没有检查是否为空。
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;
}
}
}
解决方案
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.
这篇关于的NullReferenceException WIA C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文