Re: WIA - Problem with Com
- From: "David" <david.colliver.NEWS@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 16 Dec 2009 08:23:37 -0000
Hello,
I know that WIA is not everyones speciality. Unfortunately, there is not a
lot of info on the net about using it within .NET and what I do find is not
quite suitable for what I am trying to achieve.
However, my main question here is am I using COM correctly? Am I closing it
down properly so that I can re-use it later?
Any WIA responses would be a bonus.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"David" <david.colliver.NEWS@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OJOXpPefKHA.2160@xxxxxxxxxxxxxxxxxxxxxxx
Hi all,
I am having problems with the WIA Com object and I am not sure what I am
doing with it...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WIA;
using System.Runtime.InteropServices;
using System.Configuration;
namespace Scan
{
public partial class Test : Form
{
private const int WIA_DPS_DOCUMENT_HANDLING_SELECT_FEEDER = 1;
private const int WIA_DPS_DOCUMENT_HANDLING_SELECT_FLATBED = 2;
private const int WIA_DPS_DOCUMENT_HANDLING_STATUS_FEED_READY = 1;
private const int WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS = 1024;
private const int WIA_PROPERTIES_WIA_DIP_FIRST = 2;
private const int WIA_PROPERTIES_WIA_DPA_FIRST =
WIA_PROPERTIES_WIA_DIP_FIRST + WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;
private const int WIA_PROPERTIES_WIA_DPC_FIRST =
WIA_PROPERTIES_WIA_DPA_FIRST + WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;
private const int WIA_PROPERTIES_WIA_DPS_FIRST =
WIA_PROPERTIES_WIA_DPC_FIRST + WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;
private Object WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_STATUS =
WIA_PROPERTIES_WIA_DPS_FIRST + 13;
private Object WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_SELECT =
WIA_PROPERTIES_WIA_DPS_FIRST + 14;
WIA.CommonDialog dlg = new WIA.CommonDialogClass();
Device Scanner;
public Test()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
RunScan();
}
private void Test_FormClosed(object sender, FormClosedEventArgs e)
{
if (dlg != null)
Marshal.ReleaseComObject(dlg);
if (Scanner != null)
Marshal.ReleaseComObject(Scanner);
}
private void Test_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Scanner =
dlg.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
}
private void RunScan()
{
int ScanCount = 0;
bool ReadyForScan = IsPaperInFeeder();
ImageFile[] img = null; // = new ImageFile[]();
while (ReadyForScan)
{
WIA.Item scanItem = Scanner.Items[1];
img[ScanCount] =
(ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatJPEG);
int FrameCount = img[ScanCount].FrameCount;
ReadyForScan = IsPaperInFeeder();
string scanTime = DateTime.Now.ToFileTime().ToString();
string FileName = "c:\\tempimage_" + scanTime + ".jpg";
img[ScanCount].SaveFile(FileName);
Marshal.ReleaseComObject(scanItem);
ScanCount++;
}
Marshal.ReleaseComObject(img);
}
public bool IsPaperInFeeder()
{
if (Scanner == null) throw new Exception("Scanner device must
be selected and connected!");
//get the two properties
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
//foreach (Property prop in Device.Properties)
foreach (Property prop in Scanner.Properties)
{
if (prop.PropertyID ==
(int)WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID ==
(int)WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
bool hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null) //may not exist on flatbed
scanner but required for feeder
{
//check for document feeder
int feederFlag =
unchecked((int)documentHandlingSelect.get_Value());
if ((feederFlag & WIA_DPS_DOCUMENT_HANDLING_SELECT_FEEDER)
!= 0)
{
int feedReadyFlag =
unchecked((int)documentHandlingStatus.get_Value());
hasMorePages = ((feedReadyFlag &
WIA_DPS_DOCUMENT_HANDLING_STATUS_FEED_READY) != 0);
}
}
return hasMorePages;
}
}
}
I am getting Object reference not set to an instance of an object...
Use the "new" keyword to create an object instance
This is on the line img[ScanCount] =
(ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatJPEG);
I have a breakpoint in on that line. It breaks, I click step into, the
scanner then scans and partially ejects the last document, but then I get
a green break on that line with the above error.
If I try to step again, I get the green break on the same line with "Value
does not fall within the expected range"
Step again, the project stopped, but the scanner is still saying
"Scanning"
I have found some VB.NET code and I am trying to convert it to C# and to
fit my application. The above code is my test application so that I can
give you as much working code as I can.
The VB.NET code is at http://www.xtremevbtalk.com/showthread.php?t=288758
(message 8). While mine is slightly different, I have tried to implement
some of what it is suggesting.
Also, I am not sure if I am disposing of the WIA object properly, I have
tried a number of things, which includes the Marshal.ReleaseComObject and
also just setting the object to null. Now, this is an area I am not
familiar at all.
The scanner is a HP Scanjet N6350, network scanner with an ADF. I need to
be able to scan all the documents via the ADF, as mentioned above, the
scanItem.Transfer will trigger the scan, I have 3 docs loaded and it
passes all docs through but partially holds onto the third, whilst still
saying "Scanning" on the scanner panel.
Like always, any help or pointers would be very much appreciated.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
.
- Follow-Ups:
- Re: WIA - Problem with Com
- From: Peter Duniho
- Re: WIA - Problem with Com
- References:
- WIA - Problem with Com
- From: David
- WIA - Problem with Com
- Prev by Date: Re: Applying ++ and += to properties
- Next by Date: Re: Disable selection of a Win Form
- Previous by thread: WIA - Problem with Com
- Next by thread: Re: WIA - Problem with Com
- Index(es):
Relevant Pages
|