LEADTOOLS使用教程:從TWAIN源獲取圖像
根據(jù)以下步驟,您可以創(chuàng)建一個簡單的LeadTools應(yīng)用程序,它可以從TWAIN源獲取圖像:
1. 打開Visual Studio .NET。
2. 點擊 文件->新建->項目…。
3. 打開新建項目對話框后,在模板中選擇"Visual C#"或"Visual Basic",隨后選擇"Windows窗體應(yīng)用程序"。在名稱欄中輸入項目名稱"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添加到應(yīng)用程序中。
5. 將Form1調(diào)整到設(shè)計視圖,在工具箱(視圖->工具箱)拖拽一個RasterImageViewer實例至窗體。若您的工具箱沒有RasterImageViewer,點擊工具->選擇工具箱項…。點擊瀏覽從"<LEADTOOLS_INSTALLDIR>\Bin\DotNet\Win32"中選擇Leadtools.WinForms.DLL,點擊打開并確定。
6. 切換至Form1的設(shè)計視圖,從工具箱(視圖->工具箱)里拖拽3個RadioButton控件的實例至窗體上,根據(jù)以下表格修改屬性:
Text |
Name |
Checked |
本機(jī) |
radioNative |
False |
存儲器 |
radioMemory |
False |
文件 |
radioFile |
False |
7. 從工具箱(視圖->工具箱)拖拽4個Button控件的實例至窗體上,根據(jù)以下表格修改相關(guān)屬性:
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. 編譯并運行程序。
轉(zhuǎn)載來自http://blog.gcpowertools.com.cn/post/2014/09/09/acquire-an-image-by-leadtools.aspx