Windows图像获取-在C#中设置设备属性

发布于 2024-07-22 21:22:50 字数 463 浏览 11 评论 0原文

我有一个 C# (WinForm) 程序,支持使用 WIA 进行扫描。 我试图在扫描一份或多份文档之前设置设备属性。 我主要想设置扫描仪的纸张尺寸。 以下是代码片段:

foreach (Property property in selectedDevice.Properties)
{
   //WiaProperties.WiaDpsHorizontalBedSize is my constant
   if (property.PropertyID == WiaProperties.WiaDpsHorizontalBedSize)
   {
       //Set property value here...             
   }
}

我正在查找“水平床尺寸”属性,但问题是如何将其设置为一个值? 属性中有一个 set_Value 属性,但它似乎引用了结果对象。 所以我不知道如何在设备上设置属性?

I have a C# (WinForm) program that supports scanning using WIA. I am trying to set device properties before scanning one or more documents. Primarily I want to set the paper size for the scanner. Following is a snippet of the code:

foreach (Property property in selectedDevice.Properties)
{
   //WiaProperties.WiaDpsHorizontalBedSize is my constant
   if (property.PropertyID == WiaProperties.WiaDpsHorizontalBedSize)
   {
       //Set property value here...             
   }
}

I am finding the Horizontal Bed Size property, but the question is how do I set it to a value? There is a set_Value property off of property but that seems to take a ref to a result object. So I am at a loss as to how can I set properties on a device?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2024-07-29 21:22:50

我也在做一个扫描项目,WIA 的例子很少。 您需要这段代码来设置床尺寸、DPI 等。查看 SetProperty 方法,其中包含有关如何处理 set_Value 的示例。

class Scan
{
    // Scanner only device properties (DPS)
    public const int WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const int WIA_DIP_FIRST = 2;
    public const int WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
    public const int WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
    public const int FEEDER = 1;
    public const int FLATBED = 2;
    public const int DUPLEX = 4;
    public const int FEED_READY = 1;

    WIA.CommonDialog _dialog = new WIA.CommonDialog();
    WIA.Device _scanner;

    public void ADFScan()
    {

        _dialog = new CommonDialogClass();
        _scanner = _dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);


        foreach (Property item in _scanner.Items[1].Properties)
        {
            switch (item.PropertyID)
            {
                case 6146: //4 is Black-white,gray is 2, color 1
                    SetProperty(item, 2);
                    break;
                case 6147: //dots per inch/horizontal 
                    SetProperty(item, 100);
                    break;
                case 6148: //dots per inch/vertical 
                    SetProperty(item, 100);
                    break;
                case 6149: //x point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6150: //y-point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6151: //horizontal exent 
                    SetProperty(item, (int)(8.5 * 100));
                    break;
                case 6152: //vertical extent 
                    SetProperty(item, 11 * 100);
                    break;
            }
        }
        ImageFile image = (ImageFile)_scanner.Items[1].Transfer(FormatID.wiaFormatPNG);
        System.IO.File.Delete("tmp.png");
        image.SaveFile("tmp.png");
    }

    private void SetProperty(Property property, int value)
    {
        IProperty x = (IProperty)property;
        Object val = value;
        x.set_Value(ref val);
    }


    public void test()
    {
        bool WantsToScan = true;
        while (WantsToScan) ScanAndSaveOnePage();

    }


    static void Main(string[] args)
    {
        new Scan().test();
    }
}

I am working on a scanning project as well, and there are very few WIA examples. This code is what you are looking for to set the bed size, DPI, etc. Check out the SetProperty method with an example on how to deal with set_Value.

class Scan
{
    // Scanner only device properties (DPS)
    public const int WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const int WIA_DIP_FIRST = 2;
    public const int WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const int WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
    public const int WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
    public const int FEEDER = 1;
    public const int FLATBED = 2;
    public const int DUPLEX = 4;
    public const int FEED_READY = 1;

    WIA.CommonDialog _dialog = new WIA.CommonDialog();
    WIA.Device _scanner;

    public void ADFScan()
    {

        _dialog = new CommonDialogClass();
        _scanner = _dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);


        foreach (Property item in _scanner.Items[1].Properties)
        {
            switch (item.PropertyID)
            {
                case 6146: //4 is Black-white,gray is 2, color 1
                    SetProperty(item, 2);
                    break;
                case 6147: //dots per inch/horizontal 
                    SetProperty(item, 100);
                    break;
                case 6148: //dots per inch/vertical 
                    SetProperty(item, 100);
                    break;
                case 6149: //x point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6150: //y-point where to start scan 
                    SetProperty(item, 0);
                    break;
                case 6151: //horizontal exent 
                    SetProperty(item, (int)(8.5 * 100));
                    break;
                case 6152: //vertical extent 
                    SetProperty(item, 11 * 100);
                    break;
            }
        }
        ImageFile image = (ImageFile)_scanner.Items[1].Transfer(FormatID.wiaFormatPNG);
        System.IO.File.Delete("tmp.png");
        image.SaveFile("tmp.png");
    }

    private void SetProperty(Property property, int value)
    {
        IProperty x = (IProperty)property;
        Object val = value;
        x.set_Value(ref val);
    }


    public void test()
    {
        bool WantsToScan = true;
        while (WantsToScan) ScanAndSaveOnePage();

    }


    static void Main(string[] args)
    {
        new Scan().test();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文