我如何关闭一个文件流后,它创建了一个文件c# . net,而不是使用它作为一个进程
本文关键字:一个 文件 进程 net 创建 何关闭 | 更新日期: 2023-09-27 18:08:51
我有这个方法为我的网络摄像头应用程序,我目前正在开发,我有一些问题,在它拍了一张照片后,你不能对图片做任何事情,因为它被另一个进程使用,所以只要应用程序正在运行,我就不能对我的PC上的图像做任何事情,因为它被另一个进程使用。所以如果我想再拍一张照片,它会给我这个错误…
System.IO类型的异常。在mscorlib.dll中发生IOException'但未在用户代码
中处理
我试着做流。很接近,但并没有真正改变它。
这里我有两个方法基本上做同样的事情,但它们都不工作。我应该async &等待的方法还是有点极端?
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
调用过时了。