所以我目前正在开发一个简单的应用程序,它将截取我的桌面,然后我可以通过电子邮件快速发送给朋友或家人。
虽然这是事情.. 当我按下Inilizalize按钮时,它会运行这些方法..
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
需要截图>将屏幕截图设置为图像容器中的源>将什么保存在图像容器中>将它发送给我想要发送给谁的人。
简单吧?
一切都很好,让我说我只是通过拍照并保存它,它可以每次按下时更换(覆盖)旧照片。设置图像>保存屏幕截图
但是一发送它(添加sendScreenImage();方法)我不能再覆盖旧图像,因为它说正在使用该进程
错误消息:进程无法访问该文件,因为另一个文件正在使用该文件 过程
并且它总是把这个错误扔到这一行的同一个地方
using (FileStream stream = new FileStream(path, FileMode.Create))
在saveScreenshot();
中using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Imaging;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Interop;
using System.Windows.Threading;
using WPFCSharpWebCam;
namespace WebcamApplication
{
/// <summary>
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region methods
private void initializeToolBtn_Click(object sender, RoutedEventArgs e)
{
screenshot();
setImage();
saveScreenshot();
sendScreenImage();
}
private void screenshot()
{
SendKeys.SendWait("{PRTSC}");
}
private void setImage()
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
}
private void saveScreenshot()
{
try
{
string path;
path = "%AppData%\\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)image.Source));
using (FileStream stream = new FileStream(path, FileMode.Create))
encoder.Save(stream);
System.Windows.MessageBox.Show("Replaced the old screenshot");
return;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(" saveScreenshot() went wrong " + ex.ToString());
}
}
private void sendScreenImage()
{
try
{
System.Windows.Clipboard.GetImage();
var clipboardImage = System.Windows.Clipboard.GetImage();
image.Source = clipboardImage;
string path;
path = "%AppData%\\image.png"; // collection of paths
path = Environment.ExpandEnvironmentVariables(path);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(emailfromTextbox.Text, "Screenshot Image");
msg.To.Add(new MailAddress(emailTextbox.Text));
msg.Subject = "SCreenshot Image";
msg.Body = clipboardImage.ToString();
msg.IsBodyHtml = true;
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");
//create the LinkedResource(embedded image)
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "image.png";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);
msg.AlternateViews.Add(plainView);
msg.AlternateViews.Add(htmlView);
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new NetworkCredential(emailTextbox.Text, emailpasswordTextbox.Text);
smtp.EnableSsl = true;
smtp.Send(msg);
System.Windows.MessageBox.Show("Done");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.ToString(), "GMAIL Error" + ex.ToString());
}
}
答案 0 :(得分:0)
LinkedResource
拥有一个文件流来阅读附件。
发送电子邮件后,您需要将其置于using
块中。