0

i downloaded images from my deployed website and save it into my WPF app folder , basically i am running 2 platforms , a website and WPF . What i am trying to do is users uploaded their images using the web , so on the WPF side , i download the image from the web and display on my WPF app but i got this error :

The process cannot access the file 'C:\Users\apr13mpsipa\Desktop\OneOrganizer\OneOrganizer\bin\Debug\TaskImage\Fill in the blanks.jpg' because it is being used by another process.

This is the code :

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


    }

I got the error when i try to display a image on a first button click , then i display the image again on the other button . basically this happens when i try to display the image 2 times.

---EDIT ------

Updated as of baldrick's comment :

 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;

Its still giving me the same error on the using line.

what
  • 373
  • 2
  • 10
  • 20
  • Can you clarify exactly when you get the error? Is it in this code? Which line? If not, can you paste the code where the error is seen? – Baldrick Nov 18 '13 at 05:22
  • updated , pls check my edits. its on the using line – what Nov 18 '13 at 05:25
  • Can we see the WPF code for loading the image? I think the WPF image is locking the file when it loads. You might need to specify settings on the image object to stop this happening. – Baldrick Nov 18 '13 at 05:30

3 Answers3

1

In you WPF code, you might need to specify the IgnoreImageCache setting:

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

This might force it to load, and not lock the file.

The answer here deals with the same problem.

KMC
  • 19,548
  • 58
  • 164
  • 253
Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

I am guessing you are leaving the file stream open.

Not sure why you are creating a memory stream (by the way, don't forget to close memory stream), and a file stream when you already have the bytes? Why not directly write to the file using 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);
loopedcode
  • 4,863
  • 1
  • 21
  • 21
  • I wouldn't have thought this is the problem - the file stream is in a using block in the OP's code. – Baldrick Nov 18 '13 at 05:31
0

Not sure why you are creating a memory stream (by the way, don't forget to close memory stream), and a file stream when you already have the bytes? Why not directly write to the file using 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);

Thanks