进程无法访问映像文件,因为它正由另一个进程使用

时间:2013-11-18 05:14:27

标签: c# asp.net wpf image webclient

我从我部署的网站下载了图像并将其保存到我的WPF应用程序文件夹中,基本上我运行的是2个平台,一个网站和WPF。我想要做的是用户使用网络上传他们的图像,所以在WPF方面,我从网上下载图像并在我的WPF应用程序上显示但是我收到了这个错误:

  

该进程无法访问该文件   “C:\用户\ apr13mpsipa \桌面\ OneOrganizer \ OneOrganizer \ BIN \调试\ TaskImage \填写   在blanks.jpg中,因为它正被另一个进程使用。

这是代码:

 protected void DownloadData(string strFileUrlToDownload, string taskName)
    {


        WebClient client = new WebClient();
        byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);

        MemoryStream storeStream = new MemoryStream();

        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0, (int)storeStream.Length);

        storeStream.Flush();

        currentpath = System.IO.Directory.GetCurrentDirectory() + @"\TaskImage\" + taskName + ".jpg"; //folder to contain files.

        using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite)) // ERROR HERE
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(myDataBuffer, 0, (int)storeStream.Length);
            storeStream.Close();
        }

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);


    }

当我尝试在第一次按键上显示图像时出现错误,然后我在另一个按钮上再次显示图像。基本上这种情况发生在我试图显示图像2次时。

---编辑------

更新了baldrick的评论:

 DownloadData(fileUploadDirectory + daoTask.GetImage(aid, actTask.taskID).Substring(1), daoTask.GetTaskName(aid, actTask.taskID));
            Image imgActivityTask = new Image();
            imgActivityTask.Height = 90;
            imgActivityTask.Width = 90;
            imgActivityTask.Margin = new Thickness(10);

            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri(currentpath, UriKind.Absolute);
            img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            img.EndInit();
            imgActivityTask.Source = img;

它在使用线上仍然给我同样的错误。

3 个答案:

答案 0 :(得分:1)

在您的WPF代码中,您可能需要指定IgnoreImageCache设置:

yourImage.CacheOption = BitmapCacheOption.OnLoad;
yourImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache

这可能会强制它加载,而不是锁定文件。

答案here处理同样的问题。

答案 1 :(得分:0)

我猜你要把文件流打开。

不确定为什么要创建内存流(顺便说一句,不要忘记关闭内存流),以及当你已经拥有字节时的文件流?为什么不使用File.WriteAllBytes直接写入文件?

        WebClient client = new WebClient();
        byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);            
        currentpath = System.IO.Directory.GetCurrentDirectory() + @"\TaskImage\" + taskName + ".jpg"; //folder to contain files.

        File.WriteAllBytes(currentPath, myDataBuffer);

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);

答案 2 :(得分:0)

不确定为什么要创建内存流(顺便说一句,不要忘记关闭内存流),以及当你已经拥有字节时的文件流?为什么不使用File.WriteAllBytes直接写入文件?

    WebClient client = new WebClient();
    byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);            
    currentpath = System.IO.Directory.GetCurrentDirectory() + @"\TaskImage\" + taskName + ".jpg"; //folder to contain files.

    File.WriteAllBytes(currentPath, myDataBuffer);

    //The below Getstring method to get data in raw format and manipulate it as per requirement
    string download = Encoding.ASCII.GetString(myDataBuffer);

由于