mirror of
https://github.com/soukoku/ntwain.git
synced 2025-10-24 18:29:04 +08:00
Updated samples to not use sync context as well.
This commit is contained in:
@@ -18,7 +18,7 @@ namespace Tester.WPF
|
||||
public TwainVM()
|
||||
: base(TWIdentity.CreateFromAssembly(DataGroups.Image | DataGroups.Audio, Assembly.GetEntryAssembly()))
|
||||
{
|
||||
this.SynchronizationContext = SynchronizationContext.Current;
|
||||
//this.SynchronizationContext = SynchronizationContext.Current;
|
||||
}
|
||||
|
||||
private ImageSource _image;
|
||||
@@ -41,24 +41,27 @@ namespace Tester.WPF
|
||||
|
||||
protected override void OnTransferError(TransferErrorEventArgs e)
|
||||
{
|
||||
if (e.Exception != null)
|
||||
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
|
||||
if (e.Exception != 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)
|
||||
Messenger.Default.Send(new DialogMessage(e.Exception.Message, null)
|
||||
{
|
||||
Caption = "Transfer Error Exception",
|
||||
Icon = System.Windows.MessageBoxImage.Error,
|
||||
Button = System.Windows.MessageBoxButton.OK
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Caption = "Transfer Error",
|
||||
Icon = System.Windows.MessageBoxImage.Error,
|
||||
Button = System.Windows.MessageBoxButton.OK
|
||||
});
|
||||
}
|
||||
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
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
protected override void OnTransferReady(TransferReadyEventArgs e)
|
||||
@@ -81,14 +84,25 @@ namespace Tester.WPF
|
||||
|
||||
protected override void OnDataTransferred(DataTransferredEventArgs e)
|
||||
{
|
||||
ImageSource img = null;
|
||||
if (e.NativeData != IntPtr.Zero)
|
||||
{
|
||||
Image = e.NativeData.GetWPFBitmap();
|
||||
img = e.NativeData.GetWPFBitmap();
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||
{
|
||||
var img = new BitmapImage(new Uri(e.FileDataPath));
|
||||
Image = img;
|
||||
img = new BitmapImage(new Uri(e.FileDataPath));
|
||||
}
|
||||
if (img != null)
|
||||
{
|
||||
if (img.CanFreeze)
|
||||
{
|
||||
img.Freeze();
|
||||
}
|
||||
App.Current.Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
Image = img;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,42 +58,44 @@ namespace Tester.Winform
|
||||
_twain = new TwainSession(appId);
|
||||
// either set this and don't worry about threads during events,
|
||||
// or don't and invoke during the events yourself
|
||||
_twain.SynchronizationContext = SynchronizationContext.Current;
|
||||
//_twain.SynchronizationContext = SynchronizationContext.Current;
|
||||
_twain.StateChanged += (s, e) =>
|
||||
{
|
||||
Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId);
|
||||
};
|
||||
_twain.DataTransferred += (s, e) =>
|
||||
{
|
||||
//this.Invoke(new Action(() =>
|
||||
//{
|
||||
if (pictureBox1.Image != null)
|
||||
{
|
||||
pictureBox1.Image.Dispose();
|
||||
pictureBox1.Image = null;
|
||||
}
|
||||
Bitmap img = null;
|
||||
if (e.NativeData != IntPtr.Zero)
|
||||
{
|
||||
var img = e.NativeData.GetDrawingBitmap();
|
||||
if (img != null)
|
||||
pictureBox1.Image = img;
|
||||
img = e.NativeData.GetDrawingBitmap();
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(e.FileDataPath))
|
||||
{
|
||||
var img = new Bitmap(e.FileDataPath);
|
||||
pictureBox1.Image = img;
|
||||
img = new Bitmap(e.FileDataPath);
|
||||
}
|
||||
if (img != null)
|
||||
{
|
||||
this.BeginInvoke(new Action(() =>
|
||||
{
|
||||
if (pictureBox1.Image != null)
|
||||
{
|
||||
pictureBox1.Image.Dispose();
|
||||
pictureBox1.Image = null;
|
||||
}
|
||||
pictureBox1.Image = img;
|
||||
}));
|
||||
}
|
||||
//}));
|
||||
};
|
||||
_twain.SourceDisabled += (s, e) =>
|
||||
{
|
||||
//this.Invoke(new Action(() =>
|
||||
//{
|
||||
btnStopScan.Enabled = false;
|
||||
btnStartCapture.Enabled = true;
|
||||
panelOptions.Enabled = true;
|
||||
LoadSourceCaps();
|
||||
//}));
|
||||
this.BeginInvoke(new Action(() =>
|
||||
{
|
||||
btnStopScan.Enabled = false;
|
||||
btnStartCapture.Enabled = true;
|
||||
panelOptions.Enabled = true;
|
||||
LoadSourceCaps();
|
||||
}));
|
||||
};
|
||||
_twain.TransferReady += (s, e) =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user