mirror of
https://github.com/soukoku/ntwain.git
synced 2026-02-25 13:04:07 +08:00
Reorged source tree.
This commit is contained in:
39
samples/Sample.WPF/App.xaml
Normal file
39
samples/Sample.WPF/App.xaml
Normal file
@@ -0,0 +1,39 @@
|
||||
<Application x:Class="Sample.WPF.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
StartupUri="Launcher.xaml">
|
||||
<Application.Resources>
|
||||
<ResourceDictionary>
|
||||
<!-- Launcher Style, anything you won't use goes -->
|
||||
<Style TargetType="{x:Type Rectangle}" />
|
||||
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="/ModernWPF;component/Themes/ModernBaseDesktop.xaml" />
|
||||
<ResourceDictionary Source="/ModernWPF;component/Themes/ModernLight.xaml" />
|
||||
<ResourceDictionary Source="/ModernWPF;component/Themes/ModernStyles.xaml" />
|
||||
|
||||
<ResourceDictionary>
|
||||
<Style x:Key="AppWindow" TargetType="Window" BasedOn="{StaticResource ModernWindowV2}"/>
|
||||
<Style x:Key="AppListBoxItem" TargetType="ListBoxItem" BasedOn="{StaticResource ModernListBoxItem}">
|
||||
<Setter Property="BorderThickness" Value="1"/>
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Background" Value="{DynamicResource ModernBackground2}"/>
|
||||
</Trigger>
|
||||
<Trigger Property="IsSelected" Value="True">
|
||||
<Setter Property="Background" Value="{DynamicResource ModernBackground2}"/>
|
||||
<Setter Property="Foreground" Value="{DynamicResource ModernForeground}"/>
|
||||
<Setter Property="BorderBrush" Value="{DynamicResource ModernAccent}"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
<Style x:Key="AppListBox" TargetType="ListBox" BasedOn="{StaticResource ModernListBox}">
|
||||
<Setter Property="ItemContainerStyle" Value="{StaticResource AppListBoxItem}"/>
|
||||
<Setter Property="BorderThickness" Value="0 1 1 0"/>
|
||||
<Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
17
samples/Sample.WPF/App.xaml.cs
Normal file
17
samples/Sample.WPF/App.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using ModernWPF;
|
||||
using System.Windows;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
ModernTheme.ApplyTheme(ModernTheme.Theme.Light, Accent.Green);
|
||||
base.OnStartup(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
samples/Sample.WPF/Launcher.xaml
Normal file
13
samples/Sample.WPF/Launcher.xaml
Normal file
@@ -0,0 +1,13 @@
|
||||
<Window x:Class="Sample.WPF.Launcher"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Scan Launcher" Height="200" Width="250"
|
||||
ResizeMode="NoResize" Style="{StaticResource AppWindow}">
|
||||
<Grid>
|
||||
<StackPanel VerticalAlignment="Center">
|
||||
<Button Content="Open scan window" Click="Button_Click" Margin="4 0"></Button>
|
||||
<TextBlock Text="This is to test opening/closing multiple twain sessions in an app" Margin="8"
|
||||
TextWrapping="Wrap"></TextBlock>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
31
samples/Sample.WPF/Launcher.xaml.cs
Normal file
31
samples/Sample.WPF/Launcher.xaml.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Launcher.xaml
|
||||
/// </summary>
|
||||
public partial class Launcher : Window
|
||||
{
|
||||
public Launcher()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
new MainWindow().ShowDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
112
samples/Sample.WPF/MainWindow.xaml
Normal file
112
samples/Sample.WPF/MainWindow.xaml
Normal file
@@ -0,0 +1,112 @@
|
||||
<Window x:Class="Sample.WPF.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:modern="http://modernwpf"
|
||||
xmlns:proj="clr-namespace:Sample.WPF"
|
||||
Title="{Binding AppTitle}"
|
||||
Height="600" Width="900" ResizeMode="CanResizeWithGrip"
|
||||
x:Name="theWindow"
|
||||
Style="{StaticResource AppWindow}">
|
||||
<Window.Resources>
|
||||
<proj:TwainVM x:Key="vm"></proj:TwainVM>
|
||||
</Window.Resources>
|
||||
<Window.DataContext>
|
||||
<StaticResource ResourceKey="vm"></StaticResource>
|
||||
</Window.DataContext>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="auto" />
|
||||
<RowDefinition Height="auto" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Label Content="Sources"></Label>
|
||||
<ListBox x:Name="SrcList" Grid.Row="1" Grid.RowSpan="2" Width="150"
|
||||
ItemsSource="{Binding DataSources}"
|
||||
SelectedItem="{Binding SelectedSource}"
|
||||
Style="{StaticResource AppListBox}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding Name}" TextWrapping="Wrap"></TextBlock>
|
||||
<TextBlock Text="{Binding Version, StringFormat='Version {0}'}" TextWrapping="Wrap" Foreground="{DynamicResource ModernForeground2}"
|
||||
Margin="8 0 0 0"/>
|
||||
<TextBlock Text="{Binding Protocol, StringFormat='TWAIN {0}'}" Foreground="{DynamicResource ModernForeground2}"
|
||||
Margin="8 0 0 0"/>
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<Label Content="Caps" Grid.Column="1"></Label>
|
||||
<TextBox Text="{Binding CapFilter, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding SelectedSource}"
|
||||
Grid.Column="1" Grid.Row="1"
|
||||
modern:TextBoxUI.WatermarkText="Find cap name"></TextBox>
|
||||
<ListBox x:Name="CapList" Grid.Row="2" Grid.Column="1" Width="150"
|
||||
DataContext="{Binding SelectedSource}"
|
||||
ItemsSource="{Binding Caps}"
|
||||
SelectionChanged="CapList_SelectionChanged"
|
||||
Style="{StaticResource AppListBox}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding Name}" ></TextBlock>
|
||||
<TextBlock Text="{Binding Supports}" TextWrapping="Wrap" Foreground="{DynamicResource ModernForeground2}"
|
||||
FontStyle="Italic"/>
|
||||
</StackPanel>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<Label Content="Cap values" Grid.Column="2"></Label>
|
||||
<ListBox x:Name="CapDetailList" Grid.Row="1" Grid.RowSpan="2" Grid.Column="2" MinWidth="100"
|
||||
SelectionChanged="CapDetailList_SelectionChanged"
|
||||
ScrollViewer.CanContentScroll="True"
|
||||
Style="{StaticResource AppListBox}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding}"></TextBlock>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Grid.Column="3" >
|
||||
<Label Content="State:"></Label>
|
||||
<TextBlock Text="{Binding State}"></TextBlock>
|
||||
<Button Content="_Driver settings" Command="{Binding ShowDriverCommand}"></Button>
|
||||
<Button Content="_Start capture" Command="{Binding CaptureCommand}"></Button>
|
||||
<Slider Maximum="{Binding MaxThumbnailSize}" Minimum="{Binding MinThumbnailSize}"
|
||||
Value="{Binding ThumbnailSize}" Width="100" LargeChange="20" SmallChange="10"
|
||||
VerticalAlignment="Center"
|
||||
ToolTip="{Binding ThumbnailSize}"></Slider>
|
||||
<TextBlock Text="{Binding CapturedImages.Count, StringFormat='{}{0} pages'}"></TextBlock>
|
||||
<Button Content="_Clear pages" Command="{Binding ClearCommand}"></Button>
|
||||
</StackPanel>
|
||||
|
||||
<ListBox Grid.Row="1" Grid.RowSpan="2" Grid.Column="3"
|
||||
ItemsSource="{Binding CapturedImages}">
|
||||
<ListBox.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<WrapPanel ScrollViewer.HorizontalScrollBarVisibility="Disabled"></WrapPanel>
|
||||
</ItemsPanelTemplate>
|
||||
</ListBox.ItemsPanel>
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Border Padding="4">
|
||||
<Image Stretch="Uniform"
|
||||
RenderOptions.BitmapScalingMode="HighQuality"
|
||||
Width="{Binding ElementName=theWindow, Path=DataContext.ThumbnailSize}"
|
||||
Height="{Binding ElementName=theWindow, Path=DataContext.ThumbnailSize}"
|
||||
Source="{Binding}"></Image>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Grid>
|
||||
</Window>
|
||||
105
samples/Sample.WPF/MainWindow.xaml.cs
Normal file
105
samples/Sample.WPF/MainWindow.xaml.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using ModernWPF.Controls;
|
||||
using ModernWPF.Messages;
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
TwainVM _twainVM;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
if (!DesignerProperties.GetIsInDesignMode(this))
|
||||
{
|
||||
_twainVM = this.DataContext as TwainVM;
|
||||
|
||||
Messenger.Default.Register<RefreshCommandsMessage>(this, m => m.HandleRefreshCommands());
|
||||
Messenger.Default.Register<DialogMessage>(this, msg =>
|
||||
{
|
||||
if (Dispatcher.CheckAccess())
|
||||
{
|
||||
this.HandleDialogMessageModern(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
this.HandleDialogMessageModern(msg);
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
protected override void OnClosing(CancelEventArgs e)
|
||||
{
|
||||
e.Cancel = _twainVM.State > 4;
|
||||
base.OnClosing(e);
|
||||
}
|
||||
protected override void OnClosed(EventArgs e)
|
||||
{
|
||||
_twainVM.CloseDown();
|
||||
base.OnClosed(e);
|
||||
}
|
||||
|
||||
protected override void OnSourceInitialized(EventArgs e)
|
||||
{
|
||||
base.OnSourceInitialized(e);
|
||||
_twainVM.WindowHandle = new WindowInteropHelper(this).Handle;
|
||||
|
||||
}
|
||||
|
||||
private void CapList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
var capVM = CapList.SelectedItem as CapVM;
|
||||
if (capVM != null)
|
||||
{
|
||||
CapDetailList.ItemsSource = capVM.Get();
|
||||
CapDetailList.SelectedItem = capVM.GetCurrent();
|
||||
}
|
||||
else
|
||||
{
|
||||
CapDetailList.ItemsSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CapDetailList_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
var capVM = CapList.SelectedItem as CapVM;
|
||||
if (capVM != null)
|
||||
{
|
||||
if (capVM.Supports.HasFlag(QuerySupports.Set))
|
||||
{
|
||||
try
|
||||
{
|
||||
capVM.Set(CapDetailList.SelectedItem);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex is TargetInvocationException)
|
||||
{
|
||||
ex = ex.InnerException;
|
||||
}
|
||||
ModernMessageBox.Show(this, ex.Message, "Cannot Set", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
53
samples/Sample.WPF/Properties/AssemblyInfo.cs
Normal file
53
samples/Sample.WPF/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Sample.WPF")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Yin-Chun Wang")]
|
||||
[assembly: AssemblyProduct("Sample.WPF")]
|
||||
[assembly: AssemblyCopyright("Copyright © Yin-Chun Wang 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
//In order to begin building localizable applications, set
|
||||
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
|
||||
//inside a <PropertyGroup>. For example, if you are using US english
|
||||
//in your source files, set the <UICulture> to en-US. Then uncomment
|
||||
//the NeutralResourceLanguage attribute below. Update the "en-US" in
|
||||
//the line below to match the UICulture setting in the project file.
|
||||
|
||||
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
|
||||
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
63
samples/Sample.WPF/Properties/Resources.Designer.cs
generated
Normal file
63
samples/Sample.WPF/Properties/Resources.Designer.cs
generated
Normal file
@@ -0,0 +1,63 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.0
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace Sample.WPF.Properties {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Sample.WPF.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
117
samples/Sample.WPF/Properties/Resources.resx
Normal file
117
samples/Sample.WPF/Properties/Resources.resx
Normal file
@@ -0,0 +1,117 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
26
samples/Sample.WPF/Properties/Settings.Designer.cs
generated
Normal file
26
samples/Sample.WPF/Properties/Settings.Designer.cs
generated
Normal file
@@ -0,0 +1,26 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.0
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace Sample.WPF.Properties {
|
||||
|
||||
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")]
|
||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
|
||||
|
||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
||||
|
||||
public static Settings Default {
|
||||
get {
|
||||
return defaultInstance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
samples/Sample.WPF/Properties/Settings.settings
Normal file
7
samples/Sample.WPF/Properties/Settings.settings
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
|
||||
<Profiles>
|
||||
<Profile Name="(Default)" />
|
||||
</Profiles>
|
||||
<Settings />
|
||||
</SettingsFile>
|
||||
168
samples/Sample.WPF/Sample.WPF.csproj
Normal file
168
samples/Sample.WPF/Sample.WPF.csproj
Normal file
@@ -0,0 +1,168 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{1715C2B7-5C35-4F8B-9D9B-8D68A3D5284D}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Sample.WPF</RootNamespace>
|
||||
<AssemblyName>Sample.WPF</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject>
|
||||
</StartupObject>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationIcon>scanner.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="CommonWin32, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a0a4edcfe233918d, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\CommonWin32.2.0.5.5\lib\net35-Client\CommonWin32.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="GalaSoft.MvvmLight, Version=5.0.2.32240, Culture=neutral, PublicKeyToken=0e453835af4ee6ce, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\MvvmLightLibs.5.0.2.0\lib\net40\GalaSoft.MvvmLight.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="GalaSoft.MvvmLight.Extras, Version=5.0.2.32240, Culture=neutral, PublicKeyToken=f46ff315b1088208, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\MvvmLightLibs.5.0.2.0\lib\net40\GalaSoft.MvvmLight.Extras.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.WindowsAPICodePack">
|
||||
<HintPath>..\..\packages\Microsoft.WindowsAPICodePack-Core.1.1.0.0\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.WindowsAPICodePack.Shell">
|
||||
<HintPath>..\..\packages\Microsoft.WindowsAPICodePack-Shell.1.1.0.0\lib\Microsoft.WindowsAPICodePack.Shell.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.WindowsAPICodePack.ShellExtensions">
|
||||
<HintPath>..\..\packages\Microsoft.WindowsAPICodePack-Shell.1.1.0.0\lib\Microsoft.WindowsAPICodePack.ShellExtensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ModernWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c99d0cfbea7491ef, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\ModernWPF.1.2.9.4\lib\net40-Client\ModernWPF.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ModernWPF.Mvvm">
|
||||
<HintPath>..\..\packages\ModernWPF.Mvvm.0.7.0\lib\net40-Client\ModernWPF.Mvvm.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\MvvmLightLibs.5.0.2.0\lib\net40\System.Windows.Interactivity.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Xaml">
|
||||
<RequiredTargetFramework>4.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="WindowsBase" />
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="PresentationFramework" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ApplicationDefinition Include="App.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Launcher.xaml.cs">
|
||||
<DependentUpon>Launcher.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ViewModels\TwainVM.cs" />
|
||||
<Page Include="Launcher.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="MainWindow.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Compile Include="App.xaml.cs">
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="ViewModels\CapVM.cs" />
|
||||
<Compile Include="ViewModels\DataSourceVM.cs" />
|
||||
<Compile Include="MainWindow.xaml.cs">
|
||||
<DependentUpon>MainWindow.xaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\Settings.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Settings.settings</DependentUpon>
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
<AppDesigner Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\NTwain\NTwain.csproj">
|
||||
<Project>{0c5a6fb1-0282-4d61-8354-68deb1515001}</Project>
|
||||
<Name>NTwain</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="scanner.ico" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
102
samples/Sample.WPF/ViewModels/CapVM.cs
Normal file
102
samples/Sample.WPF/ViewModels/CapVM.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a capability as a view model.
|
||||
/// </summary>
|
||||
class CapVM
|
||||
{
|
||||
DataSource _ds;
|
||||
object _wrapper;
|
||||
MethodInfo _getMethod;
|
||||
MethodInfo _getCurrentMethod;
|
||||
MethodInfo _setMethod;
|
||||
|
||||
public CapVM(DataSource ds, CapabilityId cap)
|
||||
{
|
||||
_ds = ds;
|
||||
Cap = cap;
|
||||
|
||||
|
||||
var capName = cap.ToString();
|
||||
var wrapProperty = ds.Capabilities.GetType().GetProperty(capName);
|
||||
if (wrapProperty != null)
|
||||
{
|
||||
_wrapper = wrapProperty.GetGetMethod().Invoke(ds.Capabilities, null);
|
||||
var wrapperType = _wrapper.GetType();
|
||||
_getMethod = wrapperType.GetMethod("GetValues");
|
||||
_getCurrentMethod = wrapperType.GetMethod("GetCurrent");
|
||||
_setMethod = wrapperType.GetMethods().FirstOrDefault(m => m.Name == "SetValue");
|
||||
}
|
||||
|
||||
var supportTest = ds.Capabilities.QuerySupport(cap);
|
||||
if (supportTest.HasValue)
|
||||
{
|
||||
Supports = supportTest.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_wrapper != null)
|
||||
{
|
||||
var wrapperType = _wrapper.GetType();
|
||||
QuerySupports? supports = (QuerySupports?)wrapperType.GetProperty("SupportedActions").GetGetMethod().Invoke(_wrapper, null);
|
||||
Supports = supports.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IEnumerable Get()
|
||||
{
|
||||
if (_getMethod == null)
|
||||
{
|
||||
return _ds.Capabilities.GetValues(Cap);
|
||||
}
|
||||
return _getMethod.Invoke(_wrapper, null) as IEnumerable;
|
||||
}
|
||||
public object GetCurrent()
|
||||
{
|
||||
if (_getMethod == null)
|
||||
{
|
||||
return _ds.Capabilities.GetCurrent(Cap);
|
||||
}
|
||||
return _getCurrentMethod.Invoke(_wrapper, null);
|
||||
}
|
||||
public void Set(object value)
|
||||
{
|
||||
if (_setMethod != null && value != null)
|
||||
{
|
||||
_setMethod.Invoke(_wrapper, new object[] { value });
|
||||
}
|
||||
}
|
||||
|
||||
public object MyProperty { get; set; }
|
||||
|
||||
public CapabilityId Cap { get; private set; }
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Cap > CapabilityId.CustomBase)
|
||||
{
|
||||
return "[Custom] " + ((int)Cap - (int)CapabilityId.CustomBase);
|
||||
}
|
||||
return Cap.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public QuerySupports Supports { get; private set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
73
samples/Sample.WPF/ViewModels/DataSourceVM.cs
Normal file
73
samples/Sample.WPF/ViewModels/DataSourceVM.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using GalaSoft.MvvmLight;
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps a data source as view model.
|
||||
/// </summary>
|
||||
class DataSourceVM : ViewModelBase
|
||||
{
|
||||
public DataSource DS { get; set; }
|
||||
|
||||
public string Name { get { return DS.Name; } }
|
||||
public string Version { get { return DS.Version.Info; } }
|
||||
public string Protocol { get { return DS.ProtocolVersion.ToString(); } }
|
||||
|
||||
ICollectionView _capView;
|
||||
public DataSourceVM()
|
||||
{
|
||||
Caps = new ObservableCollection<CapVM>();
|
||||
_capView = CollectionViewSource.GetDefaultView(Caps);
|
||||
_capView.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));
|
||||
_capView.Filter = FilterCapRoutine;
|
||||
}
|
||||
|
||||
private bool FilterCapRoutine(object obj)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(CapFilter))
|
||||
{
|
||||
var vm = obj as CapVM;
|
||||
if (vm != null)
|
||||
{
|
||||
return vm.Name.IndexOf(CapFilter, System.StringComparison.OrdinalIgnoreCase) > -1;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Open()
|
||||
{
|
||||
Caps.Clear();
|
||||
var rc = DS.Open();
|
||||
//rc = DGControl.Status.Get(dsId, ref stat);
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
foreach (var c in DS.Capabilities.CapSupportedCaps.GetValues().Select(o => new CapVM(DS, o)))
|
||||
{
|
||||
Caps.Add(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string _capFilter;
|
||||
|
||||
public string CapFilter
|
||||
{
|
||||
get { return _capFilter; }
|
||||
set
|
||||
{
|
||||
_capFilter = value;
|
||||
_capView.Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<CapVM> Caps { get; private set; }
|
||||
}
|
||||
}
|
||||
326
samples/Sample.WPF/ViewModels/TwainVM.cs
Normal file
326
samples/Sample.WPF/ViewModels/TwainVM.cs
Normal file
@@ -0,0 +1,326 @@
|
||||
using CommonWin32;
|
||||
using GalaSoft.MvvmLight.Messaging;
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Collections.ObjectModel;
|
||||
using GalaSoft.MvvmLight;
|
||||
using System.Windows.Input;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using ModernWPF;
|
||||
using ModernWPF.Messages;
|
||||
|
||||
namespace Sample.WPF
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps the twain session as a view model for databinding.
|
||||
/// </summary>
|
||||
class TwainVM : ViewModelBase
|
||||
{
|
||||
public TwainVM()
|
||||
{
|
||||
DataSources = new ObservableCollection<DataSourceVM>();
|
||||
CapturedImages = new ObservableCollection<ImageSource>();
|
||||
|
||||
//this.SynchronizationContext = SynchronizationContext.Current;
|
||||
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly());
|
||||
_session = new TwainSession(appId);
|
||||
_session.TransferError += _session_TransferError;
|
||||
_session.TransferReady += _session_TransferReady;
|
||||
_session.DataTransferred += _session_DataTransferred;
|
||||
_session.SourceDisabled += _session_SourceDisabled;
|
||||
_session.StateChanged += (s, e) => { RaisePropertyChanged(() => State); };
|
||||
}
|
||||
|
||||
TwainSession _session;
|
||||
|
||||
#region properties
|
||||
|
||||
public string AppTitle
|
||||
{
|
||||
get
|
||||
{
|
||||
if (NTwain.PlatformInfo.Current.IsApp64Bit)
|
||||
{
|
||||
return "TWAIN Data Source Tester (64bit)";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "TWAIN Data Source Tester (32bit)";
|
||||
}
|
||||
}
|
||||
}
|
||||
public ObservableCollection<DataSourceVM> DataSources { get; private set; }
|
||||
private DataSourceVM _selectedSource;
|
||||
|
||||
public DataSourceVM SelectedSource
|
||||
{
|
||||
get { return _selectedSource; }
|
||||
set
|
||||
{
|
||||
if (_session.State == 4)
|
||||
{
|
||||
_session.CurrentSource.Close();
|
||||
}
|
||||
_selectedSource = value;
|
||||
RaisePropertyChanged(() => SelectedSource);
|
||||
if (_selectedSource != null)
|
||||
{
|
||||
_selectedSource.Open();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int State { get { return _session.State; } }
|
||||
|
||||
private IntPtr _winHandle;
|
||||
public IntPtr WindowHandle
|
||||
{
|
||||
get { return _winHandle; }
|
||||
set
|
||||
{
|
||||
_winHandle = value;
|
||||
if (value == IntPtr.Zero)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// use this for internal msg loop
|
||||
var rc = _session.Open();
|
||||
|
||||
// use this to hook into current app loop
|
||||
//var rc = _session.Open(new WpfMessageLoopHook(value));
|
||||
|
||||
if (rc == ReturnCode.Success)
|
||||
{
|
||||
ReloadSourcesCommand.Execute(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ICommand _showDriverCommand;
|
||||
public ICommand ShowDriverCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _showDriverCommand ?? (_showDriverCommand = new RelayCommand(() =>
|
||||
{
|
||||
if (_session.State == 4)
|
||||
{
|
||||
var rc = _session.CurrentSource.Enable(SourceEnableMode.ShowUIOnly, false, WindowHandle);
|
||||
}
|
||||
}, () =>
|
||||
{
|
||||
return _session.State == 4 && _session.CurrentSource.Capabilities.CapEnableDSUIOnly.GetCurrent() == BoolType.True;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
private ICommand _captureCommand;
|
||||
public ICommand CaptureCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _captureCommand ?? (_captureCommand = new RelayCommand(() =>
|
||||
{
|
||||
if (_session.State == 4)
|
||||
{
|
||||
//if (this.CurrentSource.ICapPixelType.Get().Contains(PixelType.BlackWhite))
|
||||
//{
|
||||
// this.CurrentSource.ICapPixelType.Set(PixelType.BlackWhite);
|
||||
//}
|
||||
|
||||
//if (this.CurrentSource.ICapXferMech.Get().Contains(XferMech.File))
|
||||
//{
|
||||
// this.CurrentSource.ICapXferMech.Set(XferMech.File);
|
||||
//}
|
||||
|
||||
var rc = _session.CurrentSource.Enable(SourceEnableMode.NoUI, false, WindowHandle);
|
||||
}
|
||||
}, () =>
|
||||
{
|
||||
return _session.State == 4;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ICommand _clearCommand;
|
||||
public ICommand ClearCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _clearCommand ?? (_clearCommand = new RelayCommand(() =>
|
||||
{
|
||||
CapturedImages.Clear();
|
||||
}, () =>
|
||||
{
|
||||
return CapturedImages.Count > 0;
|
||||
}));
|
||||
}
|
||||
}
|
||||
private ICommand _reloadSrc;
|
||||
public ICommand ReloadSourcesCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _reloadSrc ?? (_reloadSrc = new RelayCommand(() =>
|
||||
{
|
||||
DataSources.Clear();
|
||||
foreach (var s in _session.Select(s => new DataSourceVM { DS = s }))
|
||||
{
|
||||
DataSources.Add(s);
|
||||
}
|
||||
}, () =>
|
||||
{
|
||||
return _session.State > 2;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the captured images.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The captured images.
|
||||
/// </value>
|
||||
public ObservableCollection<ImageSource> CapturedImages { get; private set; }
|
||||
|
||||
public double MinThumbnailSize { get { return 50; } }
|
||||
public double MaxThumbnailSize { get { return 300; } }
|
||||
|
||||
private double _thumbSize = 150;
|
||||
public double ThumbnailSize
|
||||
{
|
||||
get { return _thumbSize; }
|
||||
set
|
||||
{
|
||||
if (value > MaxThumbnailSize) { value = MaxThumbnailSize; }
|
||||
else if (value < MinThumbnailSize) { value = MinThumbnailSize; }
|
||||
_thumbSize = value;
|
||||
RaisePropertyChanged(() => ThumbnailSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
void _session_SourceDisabled(object sender, EventArgs e)
|
||||
{
|
||||
Messenger.Default.Send(new RefreshCommandsMessage());
|
||||
}
|
||||
|
||||
void _session_TransferError(object sender, TransferErrorEventArgs e)
|
||||
{
|
||||
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
if (e.Exception != null)
|
||||
{
|
||||
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
|
||||
{
|
||||
Caption = "Transfer Error Exception",
|
||||
Icon = System.Windows.MessageBoxImage.Error,
|
||||
Button = System.Windows.MessageBoxButton.OK
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Messenger.Default.Send(new DialogMessage(string.Format("Return Code: {0}\nCondition Code: {1}", e.ReturnCode, e.SourceStatus.ConditionCode), null)
|
||||
{
|
||||
Caption = "Transfer Error",
|
||||
Icon = System.Windows.MessageBoxImage.Error,
|
||||
Button = System.Windows.MessageBoxButton.OK
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
void _session_TransferReady(object sender, TransferReadyEventArgs e)
|
||||
{
|
||||
if (_session.CurrentSource.Capabilities.ICapXferMech.GetCurrent() == XferMech.File)
|
||||
{
|
||||
var formats = _session.CurrentSource.Capabilities.ICapImageFileFormat.GetValues();
|
||||
var wantFormat = formats.Contains(FileFormat.Tiff) ? FileFormat.Tiff : FileFormat.Bmp;
|
||||
|
||||
var fileSetup = new TWSetupFileXfer
|
||||
{
|
||||
Format = wantFormat,
|
||||
FileName = GetUniqueName(Path.GetTempPath(), "twain-test", "." + wantFormat)
|
||||
};
|
||||
var rc = _session.CurrentSource.DGControl.SetupFileXfer.Set(fileSetup);
|
||||
}
|
||||
}
|
||||
|
||||
string GetUniqueName(string dir, string name, string ext)
|
||||
{
|
||||
var filePath = Path.Combine(dir, name + ext);
|
||||
int next = 1;
|
||||
while (File.Exists(filePath))
|
||||
{
|
||||
filePath = Path.Combine(dir, string.Format("{0} ({1}){2}", name, next++, ext));
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
void _session_DataTransferred(object sender, DataTransferredEventArgs e)
|
||||
{
|
||||
ImageSource img = GenerateThumbnail(e);
|
||||
if (img != null)
|
||||
{
|
||||
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
CapturedImages.Add(img);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ImageSource GenerateThumbnail(DataTransferredEventArgs e)
|
||||
{
|
||||
BitmapSource img = null;
|
||||
if (e.NativeData != IntPtr.Zero)
|
||||
{
|
||||
using (var stream = e.GetNativeImageStream())
|
||||
{
|
||||
if (stream != null)
|
||||
{
|
||||
img = stream.ConvertToWpfBitmap(300, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||
{
|
||||
img = new BitmapImage(new Uri(e.FileDataPath));
|
||||
}
|
||||
|
||||
//if (img != null)
|
||||
//{
|
||||
// // from http://stackoverflow.com/questions/18189501/create-thumbnail-image-directly-from-header-less-image-byte-array
|
||||
// var scale = MaxThumbnailSize / img.PixelWidth;
|
||||
// var transform = new ScaleTransform(scale, scale);
|
||||
// var thumbnail = new TransformedBitmap(img, transform);
|
||||
// img = new WriteableBitmap(new TransformedBitmap(img, transform));
|
||||
// img.Freeze();
|
||||
//}
|
||||
return img;
|
||||
}
|
||||
|
||||
internal void CloseDown()
|
||||
{
|
||||
if (_session.State == 4)
|
||||
{
|
||||
_session.CurrentSource.Close();
|
||||
}
|
||||
_session.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
12
samples/Sample.WPF/app.config
Normal file
12
samples/Sample.WPF/app.config
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" /></startup>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
||||
10
samples/Sample.WPF/packages.config
Normal file
10
samples/Sample.WPF/packages.config
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="CommonServiceLocator" version="1.3" targetFramework="net40-Client" />
|
||||
<package id="CommonWin32" version="2.0.5.5" targetFramework="net40-Client" />
|
||||
<package id="Microsoft.WindowsAPICodePack-Core" version="1.1.0.0" targetFramework="net40-Client" />
|
||||
<package id="Microsoft.WindowsAPICodePack-Shell" version="1.1.0.0" targetFramework="net40-Client" />
|
||||
<package id="ModernWPF" version="1.2.9.4" targetFramework="net40-Client" />
|
||||
<package id="ModernWPF.Mvvm" version="0.7.0" targetFramework="net40-Client" />
|
||||
<package id="MvvmLightLibs" version="5.0.2.0" targetFramework="net40-Client" />
|
||||
</packages>
|
||||
BIN
samples/Sample.WPF/scanner.ico
Normal file
BIN
samples/Sample.WPF/scanner.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Reference in New Issue
Block a user