Results 1 to 6 of 6

Thread: [RESOLVED] Locking Jpg File in PictureBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Resolved [RESOLVED] Locking Jpg File in PictureBox

    I am using a listbox to scroll thru a folder with jpg files.
    While scrolling down I can find one that I want to move to another folder.
    I dispose and refresh the picture box(to close the jpg file) and it always allows me to move the file.

    When I am scrolling up the listbox I see a image that I want to move.
    I dispose and refresh the picutre box(to close the jpg file) and it always gives me an IO.Exception saying that the file is in use by another process.
    PLEASE tell me what I am doing wrong........
    Attached Files Attached Files

  2. #2
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: Locking Jpg File in PictureBox

    this is due the picturbox you use is keeping the file open while it is showed in it ( i dont know why , but it is very strange - i faced it before)

    you need to unlink the picture box from the file before attemping to do anytrhing with it.

    one of the possible solutions is setting the image property to nothing.

    example

    VB Code:
    1. picture1.image = nothing
    2. application.doevents
    3. 'then do whatever you want
    4.  
    5. 'if you want to reload the image again, reload it from its new position.

    if this didn't work, because somethimes the computer needs some time before unlinking the image . you must enforce it by loading other image ( a temperoraly image, like plz wait or someting)

    rgds

  3. #3
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Locking Jpg File in PictureBox

    You can load the picture in a different way so it releases the file.
    VB Code:
    1. PictureBox1.Image = DirectCast(Image.FromFile("yourfile).Clone, Image)
    2. 'or
    3. PictureBox1.Image = New Bitmap(Image.FromFile("yourfile")) 'wosscredit
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Locking Jpg File in PictureBox

    This is a feature in the image object to save memory by reading from the file all the time, since images can be quite large.

    please see my codebank example (link below) which uses IOStreams to manually open and close image files rather than allowing it to be automatically handled

    http://www.vbforums.com/showthread.php?t=356433

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    8

    Re: Locking Jpg File in PictureBox

    That fixed it!!

    thanks Phill

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: [RESOLVED] Locking Jpg File in PictureBox

    well whether you create a new bitmap or you clone it, I suggest you dispose the old image. I dont see any of that above
    ie....
    dim img as image = image.fromfile ("path here")
    dim bmp as new bitmap (img)
    img.dispose ()

    now use the bmp object
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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