原創|使用教程|編輯:郝浩|2013-09-16 09:28:27.000|閱讀 740 次
概述:本文,我們將通過使用.NET 圖像開發包Dynamic .NET TWAIN 來加快應用程序的開發和部署,并達到掃描文檔并保存圖像至數據庫的目的。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在“大數據時代”的今天,越來越多的組織使用ECM企業內容管理系統或者RMS記錄管理系統來維護和管理文檔。數字化文檔并將其儲存在中央數據庫已經成為多數企業文檔管理流程中的重要組成部分。為了實現企業文檔管理,我們需要創建一個應用程序從掃描儀或者攝像頭來捕獲文檔圖像。本文,我們將通過使用.NET 圖像開發包Dynamic .NET TWAIN 來加快應用程序的開發和部署,并達到掃描文檔并保存圖像至數據庫的目的。
下載并安裝.NET 圖像開發包Dynamic .NET TWAIN后,在安裝目錄中,找到"DynamicDotNetTWAIN.dll" under \Bin\v2.0 and \Bin\v4.0,選擇適合自己.Net 框架的相應版本。
通過Dynamic .NET TWAIN,僅需簡短的幾行代碼,你便可從TWAIN 設備或者USB/WIA攝像頭捕獲圖像。代碼如下:
Public frmCustomizeScan() { InitializeComponent(); dynamicDotNetTwain.SupportedDeviceType = Dynamsoft.DotNet.TWAIN.Enums.EnumSupportedDeviceType.SDT_ALL; // enable capturing images from both scanners and webcams int lngNum; dynamicDotNetTwain.OpenSourceManager(); for (lngNum = 0; lngNum < dynamicDotNetTwain.SourceCount; lngNum++) { cmbSource.Items.Add(dynamicDotNetTwain.SourceNameItems(Convert.ToInt16(lngNum))); // display the available imaging devices } if (lngNum > 0) cmbSource.SelectedIndex = 0; } private void cmdScan_Click(object sender, EventArgs e) { dynamicDotNetTwain.IfAppendImage = true; AcquireImage(); // acquire images } private void AcquireImage() { dynamicDotNetTwain.SelectSourceByIndex(Convert.ToInt16(cmbSource.SelectedIndex)); dynamicDotNetTwain.IfShowUI = chkIfShowUI.Checked; dynamicDotNetTwain.OpenSource(); dynamicDotNetTwain.IfDisableSourceAfterAcquire = true; try { dynamicDotNetTwain.AcquireImage(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
Dynamic .NET TWAIN提供了HTTP Upload方法,通過該方法,你可以將掃描或捕獲的圖像上傳至Web服務器和數據庫。它所支持的文件上傳格式包括PDF、TIF、 JPG、 PNG、BMP、多頁TIF和PDF等。上傳圖像時,你可以添加額外的參數并將它們與圖像記錄一起儲存在數據庫中。代碼如下:
private void BtnUpload_Click(object sender, EventArgs e) { string strActionPage = "Upload.aspx"; // for receiving the uploaded image data on the server side string strFileName = textBox1.Text; string strFileType = textBox2.Text; string strHTTPServer = "localhost"; // the name or the IP of your HTTP Server dynamicDotNetTwain.HTTPPort = 8066; //the port number of the HTTP Server dynamicDotNetTwain.HTTPUserName = "chloe"; //user name for logging into HTTP Server dynamicDotNetTwain.HTTPPassword = "c"; dynamicDotNetTwain.SetHTTPFormField("FileType", strFileType); // pass extra text parameters when uploading image dynamicDotNetTwain.HTTPUploadAllThroughPostAsPDF(strHTTPServer,strActionPage, strFileName+".pdf"); // save the captured images as a multi-page PDF file if (dynamicDotNetTwain.ErrorCode !=ErrorCode.Succeed) { MessageBox.Show(dynamicDotNetTwain.HTTPPostResponseString); } else { MessageBox.Show("PDF saved successfully."); } }
HTTPUploadAllThroughPostAsPDF方法中所涉及到的Upload.aspx 動作頁用于接收服務器端的圖像數據。因此,一定要在你的Web 服務器中部署該文件,具體做法如下:
<%@ Page Language="c#" AutoEventWireup="false" Debug="True"%> <% try { int iFileLength; HttpFileCollection files = HttpContext.Current.Request.Files; HttpPostedFile uploadfile = files["RemoteFile"]; String strImageName = uploadfile.FileName; String strFileType = System.Web.HttpContext.Current.Request.Form["FileType"]; iFileLength = uploadfile.ContentLength; Byte[] inputBuffer = new Byte[iFileLength]; System.IO.Stream inputStream; inputStream = uploadfile.InputStream; inputStream.Read(inputBuffer,0,iFileLength); String strConnString = "Data Source=192.168.8.211;Initial Catalog=WebTwain;User ID=sa;Pwd=sa"; System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString); String SqlCmdText = "INSERT INTO tblImage (strImageName,imgImageData, strFileType) VALUES (@ImageName,@Image, @ImageType)"; System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand(SqlCmdText, sqlConnection); sqlCmdObj.Parameters.Add("@Image",System.Data.SqlDbType.Binary,iFileLength).Value = inputBuffer; sqlCmdObj.Parameters.Add("@ImageName",System.Data.SqlDbType.VarChar,255).Value = strImageName; sqlCmdObj.Parameters.Add("@ImageType", System.Data.SqlDbType.VarChar, 255).Value = strFileType; sqlConnection.Open(); sqlCmdObj.ExecuteNonQuery(); sqlConnection.Close(); } catch(Exception e) { Response.Write(e.Message); throw; } %>
為了方便起見,你可以下載。若運行示例時,出現許可證錯誤,你可以下載Dynamic .NET TWAIN的有效的免費試用版。
在運行代碼前,確保已在Web服務器上部署了Upload.aspx,并且更新了strHTTPServer, HTTPPort, HTTPUserName 和BtnUpload_Click()中的HTTPPassword。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網