Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On Form load

VB
Dim imagepath As String = "C:\Data\temp.gif"

If File.Exists(imagepath) Then
  Dim opimg As Image = Image.FromFile(imagepath)
  PictureBox1.Image = opimg
End If


later check for update/change image
VB
PictureBox1.Image = webDownloadImage(image_url, imagepath)

Public Function webDownloadImage(ByVal Url As String, Optional ByVal location As String = "C:\") As Image
    Dim webClient As New System.Net.WebClient
    Dim bytes() As Byte = webClient.DownloadData(Url)

    My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
    Return New System.Drawing.Bitmap(Stream)
  End Function


this code shows error at
My.Computer.FileSystem.WriteAllBytes(location, bytes, False)

"the file cannot access the file because it used by another process"


Please help
Posted
Comments
Herman<T>.Instance 8-Feb-12 7:00am    
always check if you can open a file exclusiv
Dave Kreskowiak 7-Mar-12 10:09am    
First, your webDownloadImage code makes no sense. Actually, both of your code snippets make no sense and are not related in any way from what we can see.

In your webDownloadImage code, are you actually trying to write to "C:\" as if it was a file?? You can't do that, nor do you check for a valid path before trying to use it. You also execute a Return New Bitmap(stream), which should return a Bitmap object, but stream is not defined anywhere in that method, so where is the stream coming from?

In your top code snippet, you use Image.FromFile. I hate this method and it should be deprecated because it trips up every newbie who doesn't know how it works. The file you specify is held OPEN for the entire lifetime of the Bitmap object that is created from it. SO, if you try to overwrite that file, you'll get the error message that you posted.

Make sure the file is not open in paint or any other image editor.
 
Share this answer
 
Comments
Espen Harlinn 8-Feb-12 9:23am    
5'ed!
Abhinav S 8-Feb-12 9:28am    
Thank you.
try disposing the bytes object.

VB
PictureBox1.Image = webDownloadImage(image_url, imagepath)

Public Function webDownloadImage(ByVal Url As String, Optional ByVal location As String = "C:\") As Image
    Dim webClient As New System.Net.WebClient
    Dim bytes As Byte = webClient.DownloadData(Url)

    bytes.Dispose()

    My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
    Return New System.Drawing.Bitmap(Stream)
  End Function
 
Share this answer
 
That process could be your own application if your code already has the file opened, but never closed it before passing the file to this method.

Also, C:\ on Vista and 7 machines is read-only to Users. If you're code is running as a normal user, this write will fail.
 
Share this answer
 
You are probably trying to write the file before the download is completed The code
VB
My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
Should be in executed in the WebClient DownloadDataCompleted event handler so that the attempt to copy the file is initiated when the download is complete.

[edit]code block was not properly displayed. The pre tag is corrected to display properly - PES [/edit]
 
Share this answer
 
v3
Comments
Dave Kreskowiak 7-Mar-12 10:11am    
That's not it as WebClient.DownloadData is a blocking call and won't return until the specified resource is completely downloaded.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900