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

时间:2016-09-24 21:31:01

标签: c# .net filestream

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

  

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");
            }
        }

}

1 个答案:

答案 0 :(得分:-1)

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

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