繁体   English   中英

如何在创建文件 C# .NET 后关闭文件流而不将其用作进程

[英]How do I close a filestream after it creates a file C# .NET and not use it as a process

我为我目前正在开发的网络摄像头应用程序使用了这种方法,但我遇到了一些问题,即在拍照后您无法对图片进行任何操作,因为它正在被另一个进程使用,只要应用程序正在运行我无法对我电脑上的图像做任何事情,因为它被另一个进程使用..所以如果我尝试拍摄另一张照片,它会抛出这个错误..

mscorlib.dll 中出现“System.IO.IOException”类型的异常,但未在用户代码中处理

我试过做 stream.Close 但它并没有真正改变它。

在这里,我有两种方法基本上做同样的事情,但它们都不起作用。 我应该异步并等待该方法还是有点极端?

private void saveCamImage()
{
    string path;
    path = "%AppData%\\img.png"; // collection of paths
    path = Environment.ExpandEnvironmentVariables(path);


    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgVideo.Source));
    using (FileStream stream = new FileStream(path, FileMode.Create))
    {
        encoder.Save(stream);
        stream.Close();
    }
}




private void threadImage()
{
    string path;
    path = "%AppData%\\img1.png"; // collection of paths
    path = Environment.ExpandEnvironmentVariables(path);

    var encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgVideo.Source));

    FileStream fs = new FileStream(path, FileMode.Create);
    encoder.Save(fs);
    fs.Close();
}

附加信息:该进程无法访问该文件,因为它正被另一个进程使用。

由于缺乏有关代码的信息,这是它的结构。 我可以拍照并保存它,如果我愿意,我也可以将它发送到我/其他人的电子邮件。 有点像一个非常老派的聊天信使。

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 SystemSecurityFile
{
    /// <summary>
    ///
    /// Application features
    ///
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        #region methods


        WebCam webcam;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            webcam = new WebCam();
            webcam.InitializeWebCam(ref imgVideo);
        }

        private void saveCamImage()
        {
            string path;
            path = "%AppData%\\img.png"; // collection of paths
            path = Environment.ExpandEnvironmentVariables(path);


            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgVideo.Source));
            using (FileStream stream = new FileStream(path, FileMode.Create))
            {
                encoder.Save(stream);
                stream.Close();
            }
        }

        private void threadImage()
        {
            string path;
            path = "%AppData%\\img.png"; // collection of paths
            path = Environment.ExpandEnvironmentVariables(path);

            var encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create((BitmapSource)imgVideo.Source));

            FileStream fs = new FileStream(path, FileMode.Create);
            encoder.Save(fs);
        }

        #endregion

        private void initializeToolBtn_Click(object sender, RoutedEventArgs e)
        {
            threadImage();
            sendCamImage();
        }
        private void sendCamImage()
        {
            try
            {
                System.Windows.Clipboard.GetImage();
                var clipboardImage = System.Windows.Clipboard.GetImage();
                image.Source = clipboardImage;

                //string filePath = "C:\\Users\\Developer\\AppData\\Roaming\\Sys32.png";

                string path;
                path = "%AppData%\\img.png"; // collection of paths
                path = Environment.ExpandEnvironmentVariables(path);

                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(emailfromTextbox.Text, "Your Webcam Image");
                msg.To.Add(new MailAddress(emailTextbox.Text));
                msg.Subject = "Your Webcam 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 = "img.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("wDone");

            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString(), "wGMAIL");

                string path;
                path = "%AppData%\\img.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("Made it down here");
            }
        }

}

您应该在using子句中使用两个流。 这将处理流(通过调用dispose()这是IDisposable一种方法)

正如您所指出的, close什么都不做,因为您已经使用了using语句并因此处理了蒸汽,从而使close调用过时了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM