diff --git a/samples/WinConsole32/Program.cs b/samples/WinConsole32/Program.cs
index 48149b9..996519c 100644
--- a/samples/WinConsole32/Program.cs
+++ b/samples/WinConsole32/Program.cs
@@ -4,102 +4,133 @@ using System.Diagnostics;
namespace WinConsole32
{
- internal class Program
- {
- static async Task Main(string[] args)
+ internal class Program
{
- var libVer = FileVersionInfo.GetVersionInfo(typeof(TwainAppSession).Assembly.Location).ProductVersion;
- Console.WriteLine($"Console sample {(TWPlatform.Is32bit ? " 32bit" : " 64bit")} on NTwain {libVer}");
-
- TwainAppSession session = new TwainAppSession();
-
- session.StateChanged += Session_StateChanged;
- session.SourceDisabled += Session_SourceDisabled1;
- session.Transferred += Session_Transferred;
-
- var sts = await session.OpenDSMAsync();
-
- if (sts.IsSuccess)
- {
- Console.WriteLine("Available data sources:");
-
- TW_IDENTITY_LEGACY firstSrc = default;
- foreach (var src in session.GetSources())
+ static async Task Main(string[] args)
{
- if (!firstSrc.HasValue) firstSrc = src;
- Console.WriteLine($"\t{src}");
- }
- Console.WriteLine();
+ var libVer = FileVersionInfo.GetVersionInfo(typeof(TwainAppSession).Assembly.Location).ProductVersion;
+ Console.WriteLine($"Console sample {(TWPlatform.Is32bit ? " 32bit" : " 64bit")} on NTwain {libVer}");
- var defaultSrc = session.DefaultSource;
- Console.WriteLine($"Default data source = {defaultSrc}");
- Console.WriteLine();
+ TwainAppSession session = new TwainAppSession();
- session.ShowUserSelect();
- Console.WriteLine($"Selected data source = {session.DefaultSource}");
- Console.WriteLine();
+ session.StateChanged += Session_StateChanged;
+ session.SourceDisabled += Session_SourceDisabled1;
+ session.Transferred += Session_Transferred;
- var targetSrc = defaultSrc.HasValue ? defaultSrc : firstSrc;
+ var sts = await session.OpenDSMAsync();
- if (targetSrc.HasValue)
- {
- TestThisSource(session, targetSrc);
- }
- else
- {
- Console.WriteLine("No data source to test.");
- Console.WriteLine();
+ if (sts.IsSuccess)
+ {
+ Console.WriteLine("Available data sources:");
+
+ TW_IDENTITY_LEGACY firstSrc = default;
+ foreach (var src in session.GetSources())
+ {
+ if (!firstSrc.HasValue) firstSrc = src;
+ Console.WriteLine($"\t{src}");
+ }
+ Console.WriteLine();
+
+ var defaultSrc = session.DefaultSource;
+ Console.WriteLine($"Default data source = {defaultSrc}");
+ Console.WriteLine();
+
+ session.ShowUserSelect();
+ Console.WriteLine($"Selected data source = {session.DefaultSource}");
+ Console.WriteLine();
+
+ var targetSrc = defaultSrc.HasValue ? defaultSrc : firstSrc;
+
+ if (targetSrc.HasValue)
+ {
+ TestThisSource(session, targetSrc);
+ }
+ else
+ {
+ Console.WriteLine("No data source to test.");
+ Console.WriteLine();
+ }
+
+ Console.WriteLine("---------------------------------------------");
+ Console.WriteLine("Test in progress, press Enter to stop testing");
+ Console.WriteLine("---------------------------------------------");
+ Console.ReadLine();
+ session.TryStepdown(STATE.S1);
+ }
+ else
+ {
+ Console.WriteLine("Failed to attach: " + sts);
+ }
+
+ Console.WriteLine("-------------------");
+ Console.WriteLine("Press Enter to exit");
+ Console.WriteLine("-------------------");
+ Console.ReadLine();
}
- Console.WriteLine("---------------------------------------------");
- Console.WriteLine("Test in progress, press Enter to stop testing");
- Console.WriteLine("---------------------------------------------");
- Console.ReadLine();
- session.TryStepdown(STATE.S1);
- }
- else
- {
- Console.WriteLine("Failed to attach: " + sts);
- }
+ static int xferCount = 0;
+ static Stopwatch watch;
+ private static void Session_Transferred(TwainAppSession sender, TransferredEventArgs e)
+ {
+ if (e.Data != null)
+ {
+ var saveFile = $"twain_{DateTime.Now:yyyyMMdd_HHmmss}_{xferCount}";
+ Console.WriteLine("SUCCESS! Got twain data #{0} on thread {1}, saving to {saveFile}.", ++xferCount, Environment.CurrentManagedThreadId, saveFile);
- Console.WriteLine("-------------------");
- Console.WriteLine("Press Enter to exit");
- Console.WriteLine("-------------------");
- Console.ReadLine();
+ using (var img = new ImageMagick.MagickImage(e.Data.AsStream()))
+ {
+ var format = ImageMagick.MagickFormat.Png;
+ if (img.ColorType == ImageMagick.ColorType.Palette)
+ {
+ // bw or gray
+ saveFile += ".png";
+ }
+ else
+ {
+ // color
+ saveFile += ".jpg";
+ format = ImageMagick.MagickFormat.Jpeg;
+ img.Settings.Compression = ImageMagick.CompressionMethod.JPEG;
+ img.Quality = (uint)85;
+ }
+ img.Write(saveFile, format);
+ }
+ }
+ else
+ {
+ Console.WriteLine("BUMMER! No twain data #{0} on thread {1}.", ++xferCount, Environment.CurrentManagedThreadId);
+ }
+ e.Dispose();
+ }
+
+ private static void Session_StateChanged(TwainAppSession sender, STATE e)
+ {
+ Console.WriteLine($"Session state changed to {sender.State}");
+ }
+
+ private static void Session_SourceDisabled1(TwainAppSession sender, TW_IDENTITY_LEGACY e)
+ {
+ watch.Stop();
+ var elapsed = watch.Elapsed;
+ Console.WriteLine($"Session source disabled, took {elapsed}.");
+ sender.CloseSource();
+ }
+
+ private static void TestThisSource(TwainAppSession session, TW_IDENTITY_LEGACY source)
+ {
+ Console.WriteLine($"Testing data source {source}");
+ Console.WriteLine();
+
+ session.OpenSource(source);
+
+ session.Caps.ICAP_PIXELTYPE.Set(TWPT.RGB);
+ session.Caps.ICAP_XRESOLUTION.Set(300f);
+ session.Caps.ICAP_YRESOLUTION.Set(300f);
+ session.Caps.CAP_XFERCOUNT.Set(100);
+
+ xferCount = 0;
+ watch = Stopwatch.StartNew();
+ var rc = session.EnableSource(true, false);
+ }
}
-
- private static void Session_Transferred(TwainAppSession sender, TransferredEventArgs e)
- {
- if (e.Data != null)
- {
- Console.WriteLine("SUCCESS! Got twain data on thread {0}.", Environment.CurrentManagedThreadId);
- }
- else
- {
- Console.WriteLine("BUMMER! No twain data on thread {0}.", Environment.CurrentManagedThreadId);
- }
- e.Dispose();
- }
-
- private static void Session_StateChanged(TwainAppSession sender, STATE e)
- {
- Console.WriteLine($"Session state changed to {sender.State}");
- }
-
- private static void Session_SourceDisabled1(TwainAppSession sender, TW_IDENTITY_LEGACY e)
- {
- Console.WriteLine($"Session source disabled.");
- sender.CloseSource();
- }
-
- private static void TestThisSource(TwainAppSession session, TW_IDENTITY_LEGACY source)
- {
- Console.WriteLine($"Testing data source {source}");
- Console.WriteLine();
-
- session.OpenSource(source);
-
- var rc = session.EnableSource(true, false);
- }
- }
}
\ No newline at end of file
diff --git a/samples/WinConsole32/WinConsole32.csproj b/samples/WinConsole32/WinConsole32.csproj
index f703786..1fb12f1 100644
--- a/samples/WinConsole32/WinConsole32.csproj
+++ b/samples/WinConsole32/WinConsole32.csproj
@@ -2,12 +2,17 @@
Exe
- net8.0-windows
+ net472
enable
x86
enable
+ 12
+
+
+
+