Created
January 13, 2013 07:42
-
-
Save misodengaku/4522863 to your computer and use it in GitHub Desktop.
NTwainのサンプル丸コピペ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NTwain; | |
using NTwain.Data; | |
using NTwain.Values; | |
using System; | |
using System.Windows.Forms; | |
namespace scantest | |
{ | |
public partial class Form1 : Form | |
{ | |
TwainSession twain; | |
public Form1() | |
{ | |
InitializeComponent(); | |
SetupTwain(); | |
} | |
private void SetupTwain() | |
{ | |
TWIdentity appId = TWIdentity.Create(DataGroups.Image, new Version(1, 0), "My Company", "Test Family", "Tester", null); | |
twain = new TwainSession(appId); | |
twain.DataTransferred += (s, e) => | |
{ | |
if (e.Data != IntPtr.Zero) | |
{ | |
//_ptrTest = e.Data; | |
var img = e.Data.GetDrawingBitmap(); | |
if (img != null) | |
pictureBox1.Image = img; | |
} | |
}; | |
twain.SourceDisabled += delegate | |
{ | |
//step = "Close DS"; | |
var rc2 = twain.CloseSource(); | |
rc2 = twain.CloseManager(); | |
if (InvokeRequired) | |
{ | |
Invoke(new Action(delegate | |
{ | |
MessageBox.Show("Success!"); | |
})); | |
} | |
else | |
{ | |
MessageBox.Show("Success!"); | |
} | |
}; | |
twain.TransferReady += (s, te) => | |
{ | |
//te.CancelAll = true; | |
}; | |
Application.AddMessageFilter(twain); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
TWIdentity dsId; | |
TWStatus status = null; | |
string step = "Open DSM"; | |
// bad idea but anyway | |
IntPtr hand = this.Handle; | |
var rc = twain.OpenManager(ref hand); | |
if (rc == ReturnCode.Success) | |
{ | |
step = "User Select"; | |
rc = twain.DGControl.Identity.UserSelect(out dsId); | |
//rc = DGControl.Status.Get(ref stat); | |
//TwEntryPoint entry; | |
//rc = DGControl.EntryPoint.Get(dsId, out entry); | |
if (rc == ReturnCode.Success) | |
{ | |
step = "Open DS"; | |
rc = twain.OpenSource(dsId); | |
//rc = DGControl.Status.Get(dsId, ref stat); | |
if (rc == ReturnCode.Success) | |
{ | |
step = "Enable DS"; | |
rc = twain.EnableSource(SourceEnableMode.NoUI, false, hand); | |
return; | |
} | |
else | |
{ | |
twain.DGControl.Status.GetSource(out status); | |
} | |
} | |
else | |
{ | |
twain.DGControl.Status.GetManager(out status); | |
} | |
twain.CloseManager(); | |
} | |
else | |
{ | |
twain.DGControl.Status.GetManager(out status); | |
} | |
MessageBox.Show(string.Format("Failed at {0}: RC={1}, CC={2}", step, rc, status.ConditionCode)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment