Unlocking file when assigned to PictureBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    Unlocking file when assigned to PictureBox

    Hello,

    Say I write a picture file to: 'c:\test.jpg'

    Then I assign this picture to a picturebox
    PbFoto.Image = Image.Fromfile( "c:\test.jp g")

    Next I want the overrwrite the file test.jpg with another picture.

    When running, he says the file is in use when I want to overwrite it.
    (if not assignd to the picture box, I can overwritte as many as I want)

    I've tried 'PbFoto.Image = Nothing', but that didn't help also.

    Anyone an idea how to free the file?


  • Chris Dunaway

    #2
    Re: Unlocking file when assigned to PictureBox

    On Wed, 22 Sep 2004 13:25:03 +0200, Mark wrote:
    [color=blue]
    > Hello,
    >
    > Say I write a picture file to: 'c:\test.jpg'
    >
    > Then I assign this picture to a picturebox
    > PbFoto.Image = Image.Fromfile( "c:\test.jp g")
    >
    > Next I want the overrwrite the file test.jpg with another picture.
    >
    > When running, he says the file is in use when I want to overwrite it.
    > (if not assignd to the picture box, I can overwritte as many as I want)
    >
    > I've tried 'PbFoto.Image = Nothing', but that didn't help also.
    >
    > Anyone an idea how to free the file?[/color]

    Look elsewhere in these groups for more information, but I believe the
    solution offered is to use the FromStream method instead of using the
    Image.FromFile method. When using FromStream, the file is not locked.

    Perhaps this code can help:

    Imports System.IO
    Imports System.Drawing

    Dim fs As System.IO.FileS tream
    fs = New FileStream("C:\ test.jpg", FileMode.Open, FileAccess.Read )
    PictureBox1.Ima ge = Image.FromStrea m(fs)
    fs.Close()


    --
    Chris

    dunawayc[AT]sbcglobal_lunch meat_[DOT]net

    To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
    replace certain words in my E-Mail address.

    Comment

    • Atara

      #3
      Re: Unlocking file when assigned to PictureBox


      for memory optimization reasons GDI+ locks the file.

      ' --------------------------------------
      ' 1. Final Version:
      '! myP.Image = Image.FromFile( src)
      ' --------------------------------------
      ' 2. Edit Version:
      ' based on MSDN:
      ' Image File Is Locked When You Set the PictureBox Image
      ' Property to a File
      ' http://support.microsoft.com/default...id=kb;;Q309482
      Dim fs As System.IO.FileS tream
      fs = New System.IO.FileS tream(src, IO.FileMode.Ope n, IO.FileAccess.R ead)
      'TODO: Add exeption: 'Directory not found'
      'TODO: Add exeption: 'System.IO.File NotFoundExcepti on'
      myP.Image = System.Drawing. Image.FromStrea m(fs)
      fs.Close()
      ' --------------------------------------

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...