|
-
Apr 14th, 2007, 06:34 AM
#1
Thread Starter
Just Married
[RESOLVED] [2005] Picture Box IMage
Hi all
I want to clear the image of the picture box but it is not working when I am using file stream for loading the image.
check out how I add the image in the picture box
vb Code:
Dim fileName As String = String.Empty
Dim fileCount As Integer
Dim path As String
Dim fileStream As FileStream
'Process of Loading the company Logo 1
If Not IsDBNull(dataTable.Rows(0).Item("CompanyLogo1")) Then
Filename= String.Concat(Application.StartupPath, "\CompanyLogo\", dataTable.Rows(0).Item("CompanyLogo1").ToString.Trim)
path = System.IO.Path.Combine(Application.StartupPath, "CompanyLogo")
fileCount = IO.Directory.GetFiles(path).Length
If fileCount >= 1 And File.Exists(fileName) Then
fileStream = System.IO.File.OpenRead(Me.companyLogoName1)
fileStream.Position = 0
Me.companyLogo1.BackgroundImage = System.Drawing.Image.FromStream(fileStream) 'Loading image in the picture box
fileStream.Close()
End If
End If
But this code not clearing the background image why??
vb Code:
Me.companyLogo1.Image = Nothing
Thanks
-
Apr 14th, 2007, 06:43 AM
#2
Re: [2005] Picture Box IMage
The BackgroundImage and Image properties of the PictureBox are completely different things. If you assign an Image to the BackgroundImage property then setting the Image property to Nothing is obviously going to have no effect. If you want to clear the background image then you have to clear the BackgroundImage property. Also, make sure you dispose the Image object first if you are finished with it:
vb Code:
Me.companyLogo1.BackgroundImage.Dispose()
Me.companyLogo1.BackgroundImage = Nothing
Also, is there a particular reason that you're using the BackgroundImage property? If not then forget the FileStream altogether and just call the Load method of the PictureBox.
-
Apr 14th, 2007, 06:50 AM
#3
Thread Starter
Just Married
Re: [2005] Picture Box IMage
Also, is there a particular reason that you're using the BackgroundImage property? If not then forget the FileStream altogether and just call the Load method of the PictureBox.
Thanks sir for Reply
The reason is when I am updating the data and changing the image then the error is occuring that the process is include in the another process.
That is the reason I am using the filestream.
but some problem, I am using first time Load Method, but for showing the data again on the form I am using the filestream so is it correct?
-
Apr 14th, 2007, 06:58 AM
#4
Re: [2005] Picture Box IMage
I assume you mean that it was saying that the file is in use by another process. That would happen if you used Image.FromFile:
vb Code:
myPictureBox.Image = Image.FromFile("file path here")
because, as the documentation specifies, Image.FromFile locks the file until the Image object is disposed.
What the documentation doesn't tell you is that the Load method of the PictureBox does NOT lock the file, so if you simply use PictureBox.Load all the time you'll never have that issue.
Now, having said all that, Image.FromFile does lock the file for a reason and it is required in certain situations. If you create an Image object from a file and then call its Save method, GDI+ will try to read more data from the stream it was created from. That's why the file is locked: to provide access to that stream. If you do this:
vb Code:
myPictureBox.Image = Image.FromFile("source path here")
myPictureBox.Image.Save("target path here")
all will be well. If you do this though:
vb Code:
myPictureBox.Load("source path here")
myPictureBox.Image.Save("target path here")
you'll get a GDI+ error because the original stream is inaccessible.
The moral of the story is this: use PictureBox.Load unless doing so leads to GDI+ errors.
-
Apr 14th, 2007, 07:29 AM
#5
Thread Starter
Just Married
Re: [2005] Picture Box IMage
The moral of the story is this: use PictureBox.Load unless doing so leads to GDI+ errors.
Thanks sir
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|