2014-09-26 116 views
0

我設計了ac sharp應用程序,它使用筆記本電腦攝像頭捕獲圖像並保存,並通過Dynamsoft Dynamic .NET TWAIN 5.3 Trial ...的幫助通過電子郵件發送副本。我運行該程序從Visual Studio的工作正常,但是當我複製並粘貼.exe文件的程序目錄之外它不起作用,並顯示消息「program_name已停止工作」出現。這就是我的代碼:使用c sharp拍攝圖像

using System; 
using System.Linq; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Dynamsoft.DotNet.TWAIN.Enums; 
using Dynamsoft.DotNet.TWAIN.WebCamera; 
using System.Net.Mail; 
using System.IO; 
using System.Collections; 

namespace captureImageOfMyLaptopUser 
{ 

public partial class Form1 : Form 
{ 
    string receiverId; 
    public Form1() 
    { 
     InitializeComponent(); 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     dynamicDotNetTwain1.IfShowUI = true; 
     dynamicDotNetTwain1.SupportedDeviceType = EnumSupportedDeviceType.SDT_WEBCAM; 
     dynamicDotNetTwain1.IfThrowException = true; 
     if (!Directory.Exists("d:\\WhoOpenedMyLaptop\\Images")) 
     { 
      Directory.CreateDirectory("d:\\WhoOpenedMyLaptop"); 
     } 
     string[] filePaths = Directory.GetFiles(@"d:\WhoOpenedMyLaptop"); 
     ArrayList list = new ArrayList(); 
     int num=0; 
     foreach (string path in filePaths) 
     { 

      int l = path.Length; 

      num = int.Parse(path.Substring(24, l - 28)); 
      list.Add(num); 

     } 
     int[] array = list.ToArray(typeof(int)) as int[]; 
     if (array.Length > 0) 
     { 
      num = array.Max(); 
     } 
     else 
     { 
     num=0; 
     } 

     string fileName = "d:\\WhoOpenedMyLaptop\\pic" + (num + 1).ToString() + ".pdf"; 
     turnOnCamera(); 
     captureImage(); 
     saveImage(fileName); 
     sendingTheImage(fileName); 
    } 
    private void captureImage() 
    { 
     try 
     { 
      dynamicDotNetTwain1.EnableSource(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("error"); 
     } 
    } 
    private void turnOnCamera() 
    { 
     try 
     { 
      dynamicDotNetTwain1.SelectSourceByIndex(0) ; 
      dynamicDotNetTwain1.SetVideoContainer(pictureBox1); 
      dynamicDotNetTwain1.OpenSource(); 

      int count = dynamicDotNetTwain1.ResolutionForCamList.Count; 
      for (int j = 0; j < count; j++) 
      { 
       string tempHeight = dynamicDotNetTwain1.ResolutionForCamList[j].Height.ToString(); 
       string tempWidth = dynamicDotNetTwain1.ResolutionForCamList[j].Width.ToString(); 
       string tempResolution = tempWidth + "X" + tempHeight; 
       comboResolution.Items.Insert(j, tempResolution); 
       comboResolution.SelectedIndex = 0; 
      } 
     } 
     catch (Exception exp) 
     { 
      MessageBox.Show(exp.Message); 
     } 
    } 
    private void saveImage(string imagePath) 
    { 
     SaveFileDialog saveFileDialog = new SaveFileDialog(); 
     saveFileDialog.FileName = imagePath; 
     saveFileDialog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*"; 

     dynamicDotNetTwain1.SaveAllAsPDF(saveFileDialog.FileName); 



    } 
    private void sendingTheImage(string path) 
    { 
     readEmailIdFromTextFile(); 
     try 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

      mail.From = new MailAddress("[email protected]"); 

      mail.To.Add(receiverId); 
      string date = DateTime.Now.ToString(); 

      System.Net.Mail.Attachment attachment; 
      attachment = new System.Net.Mail.Attachment(path); 
      mail.Attachments.Add(attachment); 
      mail.Subject = "لقد قام هذا الشخص بفتح جهازك "; 
      mail.Body = "التوقيت : " + date; 

      SmtpServer.Port = 587; 
      SmtpServer.Credentials = new System.Net.NetworkCredential("seewhoopenedyourlaptop", "mylaptopcamera"); 
      SmtpServer.EnableSsl = true; 

      SmtpServer.Send(mail); 
      // MessageBox.Show("mail Send"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 

    } 
    private void readEmailIdFromTextFile() 
    { 
     String line; 
     StreamReader sr = new StreamReader("C:\\Program Files\\WhoOpenedMyLaptop\\data.txt"); 
     line = sr.ReadLine(); 
     receiverId = line; 
     sr.Close(); 

    } 

} 

}

+2

exe將不會單獨運行,你必須複製所有的輸出文件夾,其中你所有的dll的 – Charlie 2014-09-26 07:33:58

+0

@Charlie不正確。 – 2014-09-26 07:38:34

+0

是的,它現在工作,當我把DynamicDotNetTWAIN.dll在.exe文件的同一文件夾....但我如何強制用戶將該.dll複製到exe程序的同一目錄? – Boulis 2014-09-26 07:38:45

回答

2

你應該可執行文件連同所有的dll文件複製,因爲這取決於他們。您可以轉到引用,然後選擇所有庫和屬性,更改爲始終複製,然後清理項目,然後生成項目,不運行,將文件夾的所有內容複製到新位置並打開可執行文件,它應該運行