Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Picture Box IMage

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [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:
    1. Dim fileName As String = String.Empty
    2.                 Dim fileCount As Integer
    3.                 Dim path As String
    4.                 Dim fileStream As FileStream
    5.                 'Process of Loading the company Logo 1
    6.                 If Not IsDBNull(dataTable.Rows(0).Item("CompanyLogo1")) Then
    7.                     Filename= String.Concat(Application.StartupPath, "\CompanyLogo\", dataTable.Rows(0).Item("CompanyLogo1").ToString.Trim)
    8.                     path = System.IO.Path.Combine(Application.StartupPath, "CompanyLogo")
    9.                     fileCount = IO.Directory.GetFiles(path).Length
    10.                     If fileCount >= 1 And File.Exists(fileName) Then
    11.                         fileStream = System.IO.File.OpenRead(Me.companyLogoName1)
    12.                         fileStream.Position = 0
    13.                         Me.companyLogo1.BackgroundImage = System.Drawing.Image.FromStream(fileStream) 'Loading image in the picture box
    14.                         fileStream.Close()
    15.                     End If
    16.                 End If

    But this code not clearing the background image why??
    vb Code:
    1. Me.companyLogo1.Image = Nothing

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Me.companyLogo1.BackgroundImage.Dispose()
    2. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question 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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. myPictureBox.Image = Image.FromFile("source path here")
    2. myPictureBox.Image.Save("target path here")
    all will be well. If you do this though:
    vb Code:
    1. myPictureBox.Load("source path here")
    2. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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
  •  



Click Here to Expand Forum to Full Width