使用c sharp捕获图像

时间:2014-09-26 07:31:58

标签: c# dynamic

我设计的ac sharp应用程序使用笔记本电脑相机捕获图像,保存,并在Dynamsoft Dynamic .NET TWAIN 5.3试用版的帮助下通过电子邮件发送副本....当我从中运行程序时visual studio它运行正常,但是当我将.exe文件复制并粘贴到程序目录之外时,它不起作用,并且出现了一条消息,说“" program_name已停止工作"出现。这就是我的代码:

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("seewhoopenedyourlaptop@gmail.com");

            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();

    }

}

}

1 个答案:

答案 0 :(得分:2)

您应该将可执行文件与所有dll文件一起复制,因为它依赖于它们。您可以转到引用,然后选择所有库和属性,更改为始终复制,然后清理项目,然后构建项目,不运行,将文件夹的所有内容复制到新位置并打开可执行文件,它应该运行