【发布时间】:2014-04-09 11:05:25
【问题描述】:
我知道这是一个很常见的问题,我尝试使用进程,但它不起作用。所以,我希望用户能够上传照片,创建自己的头像。我所做的是拍摄选定的照片,调整其大小,然后将其裁剪并保存在应用程序/bin 中。如果他再次选择另一张照片,我会删除第一张照片并创建另一张。删除照片后,出现此错误:
The process cannot access the file : 'C:\.." because it is being used by another process.
代码:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
if (dlg.ShowDialog() == true)
{
string fileName = dlg.FileName;
Uri uri = new Uri(fileName, UriKind.RelativeOrAbsolute);
BitmapImage bit = new BitmapImage(uri);
Bitmap b = new Bitmap(fileName);
b = ResizeImage(b, 100, 100);
b = CropCircleImage(b);
string path = System.AppDomain.CurrentDomain.BaseDirectory + @"UserPhotos\";
string file =Button.Content.ToString()+".bmp";
if (File.Exists(path + file))
File.Delete(path + file);
//after it gets out from the if statement, I get the error
b.Save(path + file);
Uri newuri = new Uri(path + file, UriKind.RelativeOrAbsolute);
((ButtonLogin)sender).Image= new BitmapImage(newuri);
【问题讨论】:
-
您需要确保在运行此代码时没有保持文件打开。首先处理所有引用旧文件的位图。哦,请不要在应用程序 bin 文件夹中存储任何内容。普通用户没有对该目录的写入权限。
-
感谢您的回答!抱歉,我不明白您所说的“取消引用”是什么意思