what
what

Reputation: 373

process cannot access the image file because it is being used by another process

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.

Upvotes: 0

Views: 3684

Answers (3)

Baldrick
Baldrick

Reputation: 11840

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.

Upvotes: 1

drrewrwer
drrewrwer

Reputation: 1

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

Upvotes: 0

loopedcode
loopedcode

Reputation: 4893

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

Upvotes: 0

Related Questions