2012-06-07 54 views
0

我有使用簽名簿數據創建並將簽名bmp圖像保存到文件位置的工作代碼。我的問題是:如何修改此代碼以將圖像插入SQL Server 2008圖像字段?將簽名數據插入到MVC 2中的SQL Server中C#

以下是我的控制器從簽名平板電腦獲取簽名數據並創建一個bmp圖像並將其保存到文件位置。

 [PrincipalPermission(SecurityAction.Demand, Authenticated = true)] 
    public ActionResult SaveSignature2(IPrincipal principal) { 
     int userId = ((ScoutIdentity)principal.Identity).UserId.Value; 
     //Put user code to initialize the page here 
     SIGPLUSLib.SigPlus sigObj = new SIGPLUSLib.SigPlus(); 
     sigObj.InitSigPlus(); 
     sigObj.AutoKeyStart(); 

     string visitorname = Request.Form["visitorname"]; 
     visitorname = visitorname.Replace(" ", ""); //combines the first name with last name with no spaces 
     visitorname = visitorname.Trim(); 
     string thevrvIDstr = Request.Form["vrvID"]; 
     int thevrvID = Convert.ToInt32(thevrvIDstr); 

     //use the same data to decrypt signature 
     sigObj.AutoKeyData = Request.Form["SigText"]; 

     sigObj.AutoKeyFinish(); 
     sigObj.SigCompressionMode = 1; 
     sigObj.EncryptionMode = 2; 

     //Now, get sigstring from client 
     //Sigstring can be stored in a database if 
     //a biometric signature is desired rather than an image 
     sigObj.SigString = Request.Form["hidden"]; 

     if (sigObj.NumberOfTabletPoints() > 0) { 
      sigObj.ImageFileFormat = 0; 
      sigObj.ImageXSize = 500; 
      sigObj.ImageYSize = 150; 
      sigObj.ImagePenWidth = 8; 
      sigObj.SetAntiAliasParameters(1, 600, 700); 
      sigObj.JustifyX = 5; 
      sigObj.JustifyY = 5; 
      sigObj.JustifyMode = 5; 
      long size; 
      byte[] byteValue; 
      sigObj.BitMapBufferWrite(); 
      size = sigObj.BitMapBufferSize(); 
      byteValue = new byte[size]; 
      byteValue = (byte[])sigObj.GetBitmapBufferBytes(); 
      sigObj.BitMapBufferClose(); 
      System.IO.MemoryStream ms = new System.IO.MemoryStream(byteValue); 
      System.Drawing.Image img = System.Drawing.Image.FromStream(ms); 
      String path; 
      path = System.AppDomain.CurrentDomain.BaseDirectory; 
      path = path + "/uploadFiles/Signatures/"+thevrvIDstr+".bmp"; 
      img.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); 



      ViewData["Result"] = ("Image saved successfully to " + path); 
     } 
     else { 
      ViewData["Result"] = "signature has not been returned successfully!"; 
     } 


     ViewData["Result"] = sigObj.SigString; 
     //return RedirectToAction("vrSignIn"); 
     return View(); 
    } 

我也有其他地方的代碼在我的控制器獲取上傳的文件並將其插入到數據庫中。此代碼正常工作。

[AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult UploadTempFiles(string id, string u) 
    { 
     //this method temporarily stores files 
     string userIdString = Cryptographer.DecryptSymmetric("RijndaelManaged", SecurityImpl.UrlToBase64(u)); 
     int userId = Convert.ToInt32(userIdString); 

     if (Request.Files.Count > 0) 
     { 
      for (int i = 0; i < Request.Files.Count; i++) 
      { 
       HttpPostedFileBase file = Request.Files[i]; 
       int contentlength = file.ContentLength; 
       byte[] b = new byte[contentlength]; 
       Stream s; 
       s = file.InputStream; 
       s.Read(b, 0, contentlength); 
       VisitRequest.AddVisitorSignature(userId, b); 
      } 
     } 

     return View("UploadFiles"); 
    } 

我想,我可以使用第二個代碼的某些部分中斷映像創建過程中的第一個代碼和圖像插入到數據庫,而不是將圖像保存到一個文件位置。我不知道該怎麼做。

回答

0

我有同樣的問題,我解決了這個問題:

  1. 評論這三條線:

    //path = System.AppDomain.CurrentDomain.BaseDirectory; 
    //path = path + "/uploadFiles/Signatures/"+thevrvIDstr+".bmp"; 
    //img.Save(path, System.Drawing.Imaging.ImageFormat.Bmp); 
    
  2. 轉換圖像字符串數據(假設你的變量「strSignature」在之前聲明):

    strSignature = img.ToString(); 
    
  3. 將它與您的存儲過程一起保存。

希望這對你有所幫助!

相關問題