C#WIA ADF有下一页

C#WIA ADF有下一页,c#,wia,image-scanner,C#,Wia,Image Scanner,我已经创建了一个WIA包装,我相信它对某些人会有用。 它仍处于开发阶段,但除了HasNextPage方法不起作用之外,所有这些都起作用。我从许多不同的来源获取了代码。但基本上,在我的代码中,如果ADF中没有页面,WIA.Properties不会改变,所以它总是认为还有另一个页面?我可以做一个草率的(如果没有纸上的错误,试着接球,但那是非常草率的) 有什么想法吗 这是我的密码: ps-我不介意对编码标准的批评,请对任何能让我成为更好程序员的东西发表评论:) (我有几行代码可以尝试调试)主要问题是,

我已经创建了一个WIA包装,我相信它对某些人会有用。 它仍处于开发阶段,但除了HasNextPage方法不起作用之外,所有这些都起作用。我从许多不同的来源获取了代码。但基本上,在我的代码中,如果ADF中没有页面,WIA.Properties不会改变,所以它总是认为还有另一个页面?我可以做一个草率的(如果没有纸上的错误,试着接球,但那是非常草率的)

有什么想法吗

这是我的密码:

ps-我不介意对编码标准的批评,请对任何能让我成为更好程序员的东西发表评论:)

(我有几行代码可以尝试调试)主要问题是,无论我做什么,文档处理状态都不会改变

public struct PageSize
{
    public double Height;
    public double Width;

    public PageSize(double height, double width)
    {
        this.Height = height;
        this.Width = width;
    }
}

class WIA_PROPERTIES
{
    public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const uint WIA_DIP_FIRST = 2;
    public const uint WIA_DPA_FIRST  =  WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPC_FIRST  = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    //
    // Scanner only device properties (DPS)
    //
    public const uint WIA_DPS_FIRST    =                      WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS  =     WIA_DPS_FIRST + 13;
    public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT  =     WIA_DPS_FIRST + 14;
}



public class WiaWrapper
{

    //Image Filenames
    const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";

    //Standard Page Sizes
    public PageSize A3 = new PageSize(16.5, 11.7);
    public PageSize A4 = new PageSize(11.7, 8.3);
    public PageSize A5 = new PageSize(8.3, 5.8);
    public PageSize A6 = new PageSize(5.8, 4.1);

    public string DeviceID;

    #region Setup/select Scanner

    /// <summary>
    /// Select Scanner.
    /// If you need to save the Scanner, Save WiaWrapper.DeviceID
    /// </summary>
    public void SelectScanner()
    {
        WIA.CommonDialog wiaDiag = new WIA.CommonDialog();

        try
        {
            Device d = wiaDiag.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
            if (d != null)
            {
                DeviceID = d.DeviceID;
                return;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error, Is a scanner chosen?");
        }

        throw new Exception("No Device Selected");
    }

    /// <summary>
    /// Connect to Scanning Device
    /// </summary>
    /// <param name="deviceID"></param>
    /// <returns></returns>
    private Device Connect()
    {
        Device WiaDev = null;

        DeviceManager manager = new DeviceManager();

        //Iterate through each Device until correct Device found
        foreach (DeviceInfo info in manager.DeviceInfos)
        {
            if (info.DeviceID == DeviceID)
            {
                WIA.Properties infoprop = info.Properties;

                WiaDev = info.Connect();
                return WiaDev;
            }
        }

        throw new Exception("Scanner not found - Is it setup in DeviceID?");
    }

    #endregion

    #region Scanning utilities - hasMorePages, SetupPageSize, SetupADF, DeleteFile

    /// <summary>
    /// Check to see if ADF has more pages loaded
    /// </summary>
    /// <param name="wia"></param>
    /// <returns></returns>
    private bool HasMorePages(Device wia)
    {

        //determine if there are any more pages waiting
        Property documentHandlingSelect = null;
        Property documentHandlingStatus = null;

        string test = string.Empty;

        foreach (Property prop in wia.Properties)
        {
            string propername = prop.Name;
            string propvalue = prop.get_Value().ToString();

            test += propername + " " + propvalue + "<br>";

            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                documentHandlingSelect = prop;
            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                documentHandlingStatus = prop;
        }

        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & 0x00000001) != 0)
        {
            return ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & 0x00000001) != 0);
        }

        string tester = test;

        return false;

    }

    /// <summary>
    /// Setup Page Size
    /// </summary>
    /// <param name="wia"></param>
    private void SetupPageSize(Device wia, bool rotatePage, PageSize pageSize, int DPI, WIA.Item item)
    {

        //Setup Page Size Property
        foreach (WIA.Property itemProperty in item.Properties)
        {

            if (itemProperty.Name.Equals("Horizontal Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Vertical Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Horizontal Extent"))
            {

                double extent = DPI * pageSize.Height;

                if (rotatePage)
                {
                    extent = DPI * pageSize.Width;
                }


                ((IProperty)itemProperty).set_Value(extent);


            }
            else if (itemProperty.Name.Equals("Vertical Extent"))
            {
                double extent = DPI * pageSize.Width;

                if (rotatePage)
                {
                    extent = pageSize.Height * DPI;
                }


                ((IProperty)itemProperty).set_Value(extent);
            }

        }

    }

    /// <summary>
    /// Setup device to Use ADF if required
    /// </summary>
    private void SetupADF(Device wia, bool duplex)
    {
        string adf = string.Empty;

        foreach (WIA.Property deviceProperty in wia.Properties)
        {
            adf += deviceProperty.Name + "<br>";
            if (deviceProperty.Name == "Document Handling Select") //or PropertyID == 3088
            {
                int value = duplex ? 0x004 : 0x001;
                deviceProperty.set_Value(value);
            }

        }

    }

    private void Delete_File(string filename)
    {
        //Overwrite File
        if (File.Exists(filename))
        {
            //file exists, delete it
            File.Delete(filename);
        }

    }

    #endregion

    #region Scan Page - Main Public Method

    /// <summary>
    /// Scan Page,
    /// </summary>
    /// <param name="wia">Connected Device</param>
    /// <param name="pageSize">Page Size. A4, A3, A2 Etc</param>
    /// <param name="RotatePage">Rotation of page while scanning</param>
    public void Scan(PageSize pageSize, bool rotatePage, int DPI, string filepath, bool useAdf, bool duplex)
    {
        int pages = 0;
        bool hasMorePages = false;

        WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();

        try
        {
            do
            {
                //Connect to Device
                Device wia = Connect();
                WIA.Item item = wia.Items[1] as WIA.Item;

                //Setup ADF
                if ((useAdf) || (duplex))
                    SetupADF(wia, duplex);

                //Setup Page Size
                SetupPageSize(wia, rotatePage, pageSize, DPI,item);

                WIA.ImageFile imgFile = null;
                WIA.ImageFile imgFile_duplex = null; //if duplex is setup, this will be back page


                imgFile = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);

                //If duplex page, get back page now.
                if (duplex)
                {
                    imgFile_duplex = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);
                }

                string varImageFileName = filepath + "\\Scanned-" + pages.ToString() + ".jpg";
                Delete_File(varImageFileName); //if file already exists. delete it.
                imgFile.SaveFile(varImageFileName);

                string varImageFileName_duplex; 

                if (duplex)
                {
                    varImageFileName_duplex = filepath + "\\Scanned-" + pages++.ToString() + ".jpg";
                    Delete_File(varImageFileName_duplex); //if file already exists. delete it.
                    imgFile_duplex.SaveFile(varImageFileName);
                }

                //Check with scanner to see if there are more pages.
                if (useAdf || duplex)
                {
                    hasMorePages = HasMorePages(wia);
                    pages++;
                }

            }
            while (hasMorePages);
        }
        catch (COMException ex)
        {
            throw new Exception(CheckError((uint)ex.ErrorCode));
        }
    }

    #endregion
公共结构页面大小
{
公众双高;
公共双宽度;
公共页面大小(双高、双宽)
{
这个。高度=高度;
这个。宽度=宽度;
}
}
类WIA_属性
{
为新道具保留的公共建筑=1024;
公共警察局:第一个=2;
公共建筑WIA\u DPA\u FIRST=WIA\u DIP\u FIRST+WIA\u保留给新道具;
公共建筑WIA_DPC_FIRST=WIA_DPA_FIRST+WIA_保留用于新道具;
//
//仅扫描仪设备属性(DPS)
//
公共建筑WIA_DPS_FIRST=WIA_DPC_FIRST+WIA_保留用于新道具;
公共警察WIA\U DPS\U文件处理状态=WIA\U DPS\U优先+13;
公共警察WIA\u DPS\u文件处理\u选择=WIA\u DPS\u优先+14;
}
公共级WiaWrapper
{
//图像文件名
常量字符串wiaFormatBMP=“{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}”;
常量字符串wiaFormatPNG=“{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}”;
常量字符串wiaFormatGIF=“{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}”;
常量字符串wiaFormatJPEG=“{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}”;
常量字符串WIAFormatiff=“{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}”;
//标准页面大小
公共页面大小A3=新页面大小(16.5,11.7);
公共页面大小A4=新页面大小(11.7,8.3);
公共页面大小A5=新页面大小(8.3,5.8);
公共页面大小A6=新页面大小(5.8,4.1);
公共字符串设备ID;
#区域设置/选择扫描仪
/// 
///选择扫描仪。
///如果需要保存扫描仪,请保存WiaWrapper.DeviceID
/// 
public void SelectScanner()
{
WIA.CommonDialog wiaDiag=新WIA.CommonDialog();
尝试
{
设备d=wiaDiag.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType,true,false);
如果(d!=null)
{
DeviceID=d.DeviceID;
返回;
}
}
捕获(例外情况除外)
{
抛出新异常(“错误,是否选择了扫描仪?”);
}
抛出新异常(“未选择设备”);
}
/// 
///连接到扫描设备
/// 
/// 
/// 
专用设备连接()
{
设备WiaDev=null;
DeviceManager manager=新的DeviceManager();
//遍历每个设备,直到找到正确的设备
foreach(manager.DeviceInfo中的DeviceInfo信息)
{
如果(info.DeviceID==DeviceID)
{
WIA.Properties infoprop=info.Properties;
WiaDev=info.Connect();
返回维亚德夫;
}
}
抛出新异常(“未找到扫描仪-是否在DeviceID中设置了它?”);
}
#端区
#区域扫描实用程序-hasMorePages、SetupPageSize、SetupADF、DeleteFile
/// 
///检查ADF是否加载了更多页面
/// 
/// 
/// 
私有布尔HasMorePages(设备wia)
{
//确定是否还有其他页面等待
属性documentHandlingSelect=null;
属性documentHandlingStatus=null;
string test=string.Empty;
foreach(wia.Properties中的属性属性属性)
{
字符串propername=prop.Name;
字符串propvalue=prop.get_Value().ToString();
测试+=propername+“”+propvalue+“
”; if(prop.PropertyID==WIA\u PROPERTIES.WIA\u DPS\u DOCUMENT\u HANDLING\u SELECT) documentHandlingSelect=prop; if(prop.PropertyID==WIA\u PROPERTIES.WIA\u DPS\u文档处理\u状态) documentHandlingStatus=道具; } if((Convert.ToUInt32(documentHandlingSelect.get_Value())&0x00000001)!=0) { 返回((Convert.ToUInt32(documentHandlingStatus.get_Value())&0x00000001)!=0); } 字符串测试器=测试; 返回false; } /// ///设置页面大小 /// /// 私有无效设置页面大小(设备wia、布尔旋转页面、页面大小页面大小、int DPI、wia.Item) { //设置页面大小属性 foreach(item.Properties中的WIA.Property-itemProperty) { if(itemProperty.Name.Equals(“水平分辨率”)) { ((IProperty)itemProperty).设置_值(DPI); } else if(itemProperty.Name.Equals(“垂直分辨率”)) { ((IProperty)itemProperty).设置_值(DPI); } else if(itemProperty.Name.Equals(“水平范围”)) { 双范围=DPI*页面大小。高度; 如果(旋转页) { 范围=DPI*页面大小.宽度; } ((IProperty)itemProperty).设置_值(范围); } else if(itemProperty.Name.Equals(“垂直范围”)) { 双范围=DPI*页面大小.宽度; 如果(旋转页) { 范围=页面大小。高度*DPI; } ((IProperty)itemProperty).设置_值(范围); }