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>
|
||||
/// Closes the TWAIN data source manager.
|
||||
/// This is called when <see cref="Dispose"/> is invoked.
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<!--<PlatformTarget>x86</PlatformTarget>-->
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using NTwain;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using TWAINWorkingGroup;
|
||||
|
||||
namespace Net5Console
|
||||
{
|
||||
@@ -8,23 +10,71 @@ namespace Net5Console
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Starting twain test in console...");
|
||||
|
||||
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)
|
||||
{
|
||||
Console.WriteLine("Opened DSM");
|
||||
|
||||
Console.WriteLine("Default device:");
|
||||
Console.WriteLine($"\t{twain.DefaultDevice}");
|
||||
|
||||
Console.WriteLine("All devices:");
|
||||
TW_IDENTITY dsToUse = default;
|
||||
foreach (var dev in twain.GetDevices())
|
||||
{
|
||||
Console.WriteLine($"\t{dev}");
|
||||
if (dev.ProductName == "TWAIN2 FreeImage Software Scanner")
|
||||
{
|
||||
dsToUse = dev;
|
||||
}
|
||||
Console.WriteLine("Current device:");
|
||||
Console.WriteLine($"\t{twain.CurrentDevice}");
|
||||
twain.CurrentDevice = twain.DefaultDevice;
|
||||
Console.WriteLine("Current device after setting:");
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Reference in New Issue
Block a user