mirror of
https://github.com/soukoku/ntwain.git
synced 2025-09-18 17:47:57 +08:00
Add attempt to enable ds.
This commit is contained in:
@@ -153,6 +153,7 @@ namespace NTwain
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Closes the TWAIN data source manager.
|
/// Closes the TWAIN data source manager.
|
||||||
|
/// This is called when <see cref="Dispose"/> is invoked.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
@@ -284,6 +285,31 @@ namespace NTwain
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to show the current device's settings dialog if supported.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public STS ShowSettings()
|
||||||
|
{
|
||||||
|
TW_USERINTERFACE ui = default;
|
||||||
|
ui.hParent = _hWnd;
|
||||||
|
ui.ShowUI = 1;
|
||||||
|
return _twain.DatUserinterface(DG.CONTROL, MSG.ENABLEDSUIONLY, ref ui);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Begins the capture process on the current device.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="showUI">Whether to display settings UI. Not all devices support this.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public STS StartCapture(bool showUI)
|
||||||
|
{
|
||||||
|
TW_USERINTERFACE ui = default;
|
||||||
|
ui.hParent = _hWnd;
|
||||||
|
ui.ShowUI = (ushort)(showUI ? 1 : 0);
|
||||||
|
return _twain.DatUserinterface(DG.CONTROL, MSG.ENABLEDS, ref ui);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0-windows</TargetFramework>
|
<TargetFramework>net5.0-windows</TargetFramework>
|
||||||
|
<!--<PlatformTarget>x86</PlatformTarget>-->
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
using NTwain;
|
using NTwain;
|
||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Threading;
|
||||||
|
using TWAINWorkingGroup;
|
||||||
|
|
||||||
namespace Net5Console
|
namespace Net5Console
|
||||||
{
|
{
|
||||||
@@ -8,23 +10,71 @@ namespace Net5Console
|
|||||||
{
|
{
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("Starting twain test in console...");
|
||||||
|
|
||||||
using (var twain = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero))
|
using (var twain = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero))
|
||||||
|
using (var hold = new ManualResetEventSlim())
|
||||||
{
|
{
|
||||||
|
twain.DeviceEvent += (sender, e) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Got device event " + (TWDE)e.Event);
|
||||||
|
};
|
||||||
|
twain.ScanEvent += (sender, closing) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Got scan event " + closing);
|
||||||
|
|
||||||
|
TW_USERINTERFACE twuserinterface = default;
|
||||||
|
if (twain.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Disabled device.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Failed to disabled device.");
|
||||||
|
}
|
||||||
|
hold.Set();
|
||||||
|
};
|
||||||
|
|
||||||
if (twain.Open() == TWAINWorkingGroup.STS.SUCCESS)
|
if (twain.Open() == TWAINWorkingGroup.STS.SUCCESS)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Opened DSM");
|
Console.WriteLine("Opened DSM");
|
||||||
|
|
||||||
Console.WriteLine("Default device:");
|
Console.WriteLine("Default device:");
|
||||||
Console.WriteLine($"\t{twain.DefaultDevice}");
|
Console.WriteLine($"\t{twain.DefaultDevice}");
|
||||||
|
|
||||||
Console.WriteLine("All devices:");
|
Console.WriteLine("All devices:");
|
||||||
|
TW_IDENTITY dsToUse = default;
|
||||||
foreach (var dev in twain.GetDevices())
|
foreach (var dev in twain.GetDevices())
|
||||||
{
|
{
|
||||||
Console.WriteLine($"\t{dev}");
|
Console.WriteLine($"\t{dev}");
|
||||||
|
if (dev.ProductName == "TWAIN2 FreeImage Software Scanner")
|
||||||
|
{
|
||||||
|
dsToUse = dev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
twain.CurrentDevice = dsToUse;
|
||||||
|
if (twain.CurrentDevice.HasValue)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Current device after opening attempt:");
|
||||||
|
Console.WriteLine($"\t{twain.CurrentDevice}");
|
||||||
|
|
||||||
|
var sts = twain.StartCapture(false);
|
||||||
|
if (sts == STS.SUCCESS)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Waiting for capture to complete.");
|
||||||
|
|
||||||
|
while (!hold.IsSet) Thread.Sleep(100);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Failed to start capture: " + sts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("No devices opened.");
|
||||||
}
|
}
|
||||||
Console.WriteLine("Current device:");
|
|
||||||
Console.WriteLine($"\t{twain.CurrentDevice}");
|
|
||||||
twain.CurrentDevice = twain.DefaultDevice;
|
|
||||||
Console.WriteLine("Current device after setting:");
|
|
||||||
Console.WriteLine($"\t{twain.CurrentDevice}");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -32,7 +82,7 @@ namespace Net5Console
|
|||||||
}
|
}
|
||||||
Console.WriteLine("Test Ended");
|
Console.WriteLine("Test Ended");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user