使用c sharp捕获图像
本文关键字:图像 sharp 使用 | 更新日期: 2023-09-27 18:24:41
我设计了一个c sharp应用程序,在Dynamsoft Dynamic.NET TWAIN 5.3试用版的帮助下,该应用程序使用笔记本电脑摄像头捕捉图像,保存图像,并通过电子邮件发送副本。。。。当我从visualstudio运行程序时,它运行良好,但当我将.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();
}
}
}
您应该将可执行文件与所有dll文件一起复制,因为它依赖于它们。您可以转到引用,然后选择所有库,然后在atributes,更改为始终复制,然后清理项目,然后构建项目,不要运行,将文件夹的所有内容复制到新位置并打开可执行文件,它应该运行