LEADTOOLS使用教程:从TWAIN源获取图像
根据以下步骤,您可以创建一个简单的LeadTools应用程序,它可以从TWAIN源获取图像:
1. 打开Visual Studio .NET。
2. 点击 文件->新建->项目…。
3. 打开新建项目对话框后,在模板中选择"Visual C#"或"Visual Basic",随后选择"Windows窗体应用程序"。在名称栏中输入项目名称"Acquiring an Image",并使用"浏览"按钮选择您工程的存储路径,点击"确定"。
4. 在"解决方案资源管理器"中,右击"引用",选择"添加引用"。在"引用管理器"中,浏览选择Leadtools For .NET文件夹" <LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32",选择以下的DLL:
- Leadtools.dll
- Leadtools.Twain.dll
- Leadtools.Codecs.dll
- Leadtools.WinForms.dll
点击"确定"按钮,将以上所有的DLL添加到应用程序中。
5. 将Form1调整到设计视图,在工具箱(视图->工具箱)拖拽一个RasterImageViewer实例至窗体。若您的工具箱没有RasterImageViewer,点击工具->选择工具箱项…。点击浏览从"<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32"中选择Leadtools.WinForms.DLL,点击打开并确定。
6. 切换至Form1的设计视图,从工具箱(视图->工具箱)里拖拽3个RadioButton控件的实例至窗体上,根据以下表格修改属性:
Text |
Name |
Checked |
本机 |
radioNative |
False |
存储器 |
radioMemory |
False |
文件 |
radioFile |
False |
7. 从工具箱(视图->工具箱)拖拽4个Button控件的实例至窗体上,根据以下表格修改相关属性:
Text |
Name |
获取 |
buttonAcquire |
选择源 |
buttonSelectSource |
保存模板文件 |
buttonSaveTemplateFile |
加载模板文件 |
buttonLoadTemplateFile |
8. 切换至Form1的代码视图,在文件开始添加以下代码:
1: [C#] 2: using Leadtools; 3: using Leadtools.Twain; 4: using Leadtools.Codecs; 5: using Leadtools.WinForms;
9. 声明以下私有变量:
1: [C#] 2: private TwainSession twnSession;
10. 为Form1的Load事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void Form1_Load(object sender, System.EventArgs e) 4: { 5: try 6: { 7: twnSession = new TwainSession(); 8: twnSession.Startup(this, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None); 9: } 10: catch (Exception ex) 11: { 12: MessageBox.Show(this, ex.Message); 13: } 14: }
11. 为Form1的Closing事件添加事件句柄,代码如下:
1: [C#] 2: private void Form1_FormClosing(object sender, FormClosingEventArgs e) 3: { 4: try 5: { 6: twnSession.Shutdown(); 7: } 8: catch (Exception ex) 9: { 10: MessageBox.Show(this, ex.Message); 11: } 12: }
12. 为twnSession AcquirePage事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void twnSession_AcquirePage(object sender, TwainAcquirePageEventArgs e) 4: { 5: rasterImageViewer1.Image = e.Image; 6: }
13. 为buttonAcquire Click 事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void buttonAcquire_Click(object sender, System.EventArgs e) 4: { 5: try 6: { 7: twnSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(twnSession_AcquirePage); 8: twnSession.Acquire(TwainUserInterfaceFlags.Show); 9: } 10: catch (Exception ex) 11: { 12: MessageBox.Show(this, ex.Message); 13: } 14: }
14. 为buttonSelectSource Click事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void buttonSelectSource_Click(object sender, System.EventArgs e) 4: { 5: try 6: { 7: twnSession.SelectSource(string.Empty); 8: } 9: catch (Exception ex) 10: { 11: MessageBox.Show(this, ex.Message); 12: } 13: }
15. 为buttonSaveTemplateFile Click事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void buttonSaveTemplateFile_Click(object sender, System.EventArgs e) 4: { 5: try 6: { 7: twnSession.SaveTemplateFile(@"c:\test.ltt"); 8: } 9: catch (Exception ex) 10: { 11: MessageBox.Show(this, ex.Message); 12: } 13: }
16. 为buttonLoadTemplateFile Click事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void buttonLoadTemplateFile_Click(object sender, System.EventArgs e) 4: { 5: try 6: { 7: twnSession.LoadTemplateFile(@"c:\test.ltt"); 8: } 9: catch (Exception ex) 10: { 11: MessageBox.Show(this, ex.Message); 12: } 13: }
17. 为radioNative CheckedChanged事件添加事件句柄,并添加以下代码:
1: [C#] 2: 3: private void radioNative_CheckedChanged(object sender, System.EventArgs e) 4: { 5: try 6: { 7: TwainCapability capability = new TwainCapability(); 8: capability.Information.ContainerType = TwainContainerType.OneValue; 9: capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 10: capability.OneValueCapability.ItemType = TwainItemType.Uint16; 11: capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Native; 12: twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 13: } 14: catch (Exception ex) 15: { 16: MessageBox.Show(this, ex.Message); 17: } 18: }
18. 为radioMemory CheckedChanged事件添加事件句柄,代码如下:
1: [C#] 2: 3: private void radioMemory_CheckedChanged(object sender, System.EventArgs e) 4: { 5: try 6: { 7: TwainCapability capability = new TwainCapability(); 8: capability.Information.ContainerType = TwainContainerType.OneValue; 9: capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 10: capability.OneValueCapability.ItemType = TwainItemType.Uint16; 11: capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Memory; 12: twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 13: } 14: catch (Exception ex) 15: { 16: MessageBox.Show(this, ex.Message); 17: } 18: }
19. 为radioFile CheckedChanged事件添加事件句柄,代码如下:
1: [C#] 2: 3: private void radioFile_CheckedChanged(object sender, System.EventArgs e) 4: { 5: try 6: { 7: TwainCapability capability = new TwainCapability(); 8: capability.Information.ContainerType = TwainContainerType.OneValue; 9: capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 10: capability.OneValueCapability.ItemType = TwainItemType.Uint16; 11: capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.File; 12: twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 13: } 14: catch (Exception ex) 15: { 16: MessageBox.Show(this, ex.Message); 17: } 18: }
20. 编译并运行程序。
转载来自//blog.gcpowertools.com.cn/post/2014/09/09/acquire-an-image-by-leadtools.aspx