为什么FileMode.创建在另一个进程中使用

本文关键字:进程 另一个 FileMode 创建 为什么 | 更新日期: 2023-09-27 18:08:39

所以我目前正在开发一个简单的应用程序,它可以截取我的桌面屏幕截图,然后我可以通过电子邮件将其快速发送给朋友或家人。

然而,事情是这样的……当我按下initialize按钮时,它会运行这些方法。
        screenshot();
        setImage();
        saveScreenshot();
        sendScreenImage();

它获取截图>将截图设置为图像容器中的源>保存图像容器中的内容>将其发送给我想发送给的人。
很简单吧?

这一切都工作得很好,让我说我只是通过拍摄照片并保存它,它可以替换(覆盖)旧的,每次我按

截图> set image>保存截图

但一旦我发送它(添加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());
            }
        }

为什么FileMode.创建在另一个进程中使用

LinkedResource拥有一个文件流来读取附件

您需要在发送电子邮件后将其处置在using块中。