.net - NullReferenceException WIA C# -
when run code below error
nullreferenceexception unhandled..
below 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 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 scanner set use 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 wish scan page , save 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 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 set null when pass dialog.showselectitems(). function may trying use without checking if null.
Comments
Post a Comment