mirror of
https://github.com/soukoku/ntwain.git
synced 2026-02-25 13:04:07 +08:00
Add net5.0-windows build on x86 apps for now.
This commit is contained in:
107
samples/Sample.Net5Console/Program.cs
Normal file
107
samples/Sample.Net5Console/Program.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
|
||||
namespace Sample.Net5Console
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
if (PlatformInfo.Current.IsApp64Bit)
|
||||
{
|
||||
Console.WriteLine("[64bit]");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("[32bit]");
|
||||
}
|
||||
// just an amusing example to do twain in console without UI
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
try
|
||||
{
|
||||
DoTwainWork();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("ERROR: " + ex.ToString());
|
||||
}
|
||||
});
|
||||
Console.WriteLine("Test started, press Enter to exit.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
|
||||
|
||||
static readonly TwainSession twain = InitTwain();
|
||||
private static TwainSession InitTwain()
|
||||
{
|
||||
var twain = new TwainSession(TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly()));
|
||||
twain.TransferReady += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("Got xfer ready on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
||||
};
|
||||
twain.DataTransferred += (s, e) =>
|
||||
{
|
||||
if (e.NativeData != IntPtr.Zero)
|
||||
{
|
||||
Console.WriteLine("SUCCESS! Got twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("BUMMER! No twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
||||
}
|
||||
};
|
||||
|
||||
twain.SourceDisabled += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("Source disabled on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
||||
var rc = twain.CurrentSource.Close();
|
||||
rc = twain.Close();
|
||||
};
|
||||
return twain;
|
||||
}
|
||||
|
||||
const string SAMPLE_SOURCE = "TWAIN2 FreeImage Software Scanner";
|
||||
static void DoTwainWork()
|
||||
{
|
||||
Console.WriteLine("Getting ready to do twain stuff on thread {0}...", Thread.CurrentThread.ManagedThreadId);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
var rc = twain.Open();
|
||||
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
var hit = twain.FirstOrDefault(s => string.Equals(s.Name, SAMPLE_SOURCE));
|
||||
if (hit == null)
|
||||
{
|
||||
Console.WriteLine("The sample source \"" + SAMPLE_SOURCE + "\" is not installed.");
|
||||
twain.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = hit.Open();
|
||||
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
Console.WriteLine("Starting capture from the sample source...");
|
||||
rc = hit.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
twain.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Failed to open dsm with rc={0}!", rc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
13
samples/Sample.Net5Console/Sample.Net5Console.csproj
Normal file
13
samples/Sample.Net5Console/Sample.Net5Console.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\NTwain\NTwain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user