PictureBox "locks" file, cannot move/delete

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gerardianlewis@yahoo.com

    PictureBox "locks" file, cannot move/delete

    Any help appreciated.

    (VB.NET under XP and Vista, SP1 installed)

    My code, inherited from a VB6 version of an app that ran under W98,
    loads an image from a file into a PictureBox. The user may want to
    move or delete the file that is being shown. The corresponding event-
    driven code for "delete this picture" uses:

    My.Computer.Fil eSystem.DeleteF ile(sPath)

    The exception which is thrown tells me that the system cannot access
    the file as it's "being used by another process". What other process?
    (There is no multithreading in the app.) Presumably this means that
    you can't do anything with a jpg image file while it's being displayed
    in a PictureBox (whereas you could with VB6/W98). Annoying, but I
    decided on a workaround that meant blanking out the PictureBox before
    attempting to delete the file. I've experimented with several methods,
    including loading a new image from a different (dummy) jpg file,
    setting the Image property to a bitmap created on the fly, or just
    doing this before the delete:

    frmMainForm.pic Preview.Image.D ispose()

    Still no luck - or rather, sometimes the deletion takes place without
    problems, but most of the time it throws an exception, and if you loop
    around and try again and again for a minute or two, usually the
    exceptions just keep on coming. Still more mysteriously, tests suggest
    that the system finally frees up the image file for deletion after a
    few seconds in some cases (counting from where the PictureBox is
    loaded with another image), or after a few minutes in others.

    Can anyone figure out what's going on?

    (I know that in theory you could dispense with the PictureBox
    completely and just display the image using GDI+ graphics methods on
    the form surface, but there are various reasons why implementing such
    a solution would be hugely time-consuming for this app.)

  • Cor Ligthert[MVP]

    #2
    Re: PictureBox "locks&quo t; file, cannot move/delete

    Gerard,

    What is hugely time-consuming with drawing an Image on the surface, I am
    not aware of the fact that after the scene is another method used for
    drawing on the picturebox background surface.

    Beside that does this simple code work for me.

    PictureBox1.Loa d("d:\test.jpg" )
    IO.File.Delete( "d:\test.jp g")

    Cor


    <gerardianlewis @yahoo.comschre ef in bericht
    news:939612a1-a933-4dd7-b3f6-a417c28a3b0a@m3 6g2000hse.googl egroups.com...
    Any help appreciated.
    >
    (VB.NET under XP and Vista, SP1 installed)
    >
    My code, inherited from a VB6 version of an app that ran under W98,
    loads an image from a file into a PictureBox. The user may want to
    move or delete the file that is being shown. The corresponding event-
    driven code for "delete this picture" uses:
    >
    My.Computer.Fil eSystem.DeleteF ile(sPath)
    >
    The exception which is thrown tells me that the system cannot access
    the file as it's "being used by another process". What other process?
    (There is no multithreading in the app.) Presumably this means that
    you can't do anything with a jpg image file while it's being displayed
    in a PictureBox (whereas you could with VB6/W98). Annoying, but I
    decided on a workaround that meant blanking out the PictureBox before
    attempting to delete the file. I've experimented with several methods,
    including loading a new image from a different (dummy) jpg file,
    setting the Image property to a bitmap created on the fly, or just
    doing this before the delete:
    >
    frmMainForm.pic Preview.Image.D ispose()
    >
    Still no luck - or rather, sometimes the deletion takes place without
    problems, but most of the time it throws an exception, and if you loop
    around and try again and again for a minute or two, usually the
    exceptions just keep on coming. Still more mysteriously, tests suggest
    that the system finally frees up the image file for deletion after a
    few seconds in some cases (counting from where the PictureBox is
    loaded with another image), or after a few minutes in others.
    >
    Can anyone figure out what's going on?
    >
    (I know that in theory you could dispense with the PictureBox
    completely and just display the image using GDI+ graphics methods on
    the form surface, but there are various reasons why implementing such
    a solution would be hugely time-consuming for this app.)
    >

    Comment

    • gerardianlewis@yahoo.com

      #3
      Re: PictureBox &quot;locks&quo t; file, cannot move/delete

      On Apr 20, 11:46 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
      wrote:
      Gerard,
      >
      What is  hugely time-consuming with drawing an Image on the surface, I am
      not aware of the fact that after the scene is another method used for
      drawing on the picturebox background surface.
      >
      Beside that does this simple code work for me.
      >
      PictureBox1.Loa d("d:\test.jpg" )
      IO.File.Delete( "d:\test.jp g")
      Something similar worked for me, too, until I tested in XP/Vista,
      where the trouble started. Oddly, the exception is only thrown
      sometimes, as I said.

      I searched this newsgroup and eventually came across someone who had
      the same problem. He was told to clone the image and then discard it.
      Currently I'm testing the following (a variant of what was suggested):


      Public Sub LoadImageClone( ByVal sPath As String, ByVal picPicBox
      As PictureBox)
      Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox

      Dim bmpOriginal As New Bitmap(sPath) 'original file's bitmap,
      original size

      bmpClone = New Bitmap(bmpOrigi nal.Width, bmpOriginal.Hei ght)
      'create clone, initially empty, same size

      Dim gr As Graphics = Graphics.FromIm age(bmpClone) 'get object
      representing clone's currently empty drawing surface
      gr.DrawImage(bm pOriginal, 0, 0) 'copy original image onto this
      surface
      gr.Dispose()

      bmpOriginal.Dis pose()

      picPicBox.Image = bmpClone 'assign the clone to picture box

      End Sub

      It seems to work around 90% of the time, but there are occasional
      sizing problems. I'll post the fix if I can figure it out. Thanks for
      replying.

      >
      Cor
      >
      <gerardianle... @yahoo.comschre ef in berichtnews:939 612a1-a933-4dd7-b3f6-a417c28a3b0a@m3 6g2000hse.googl egroups.com...
      >
      >
      >
      Any help appreciated.
      >
      (VB.NET under XP and Vista, SP1 installed)
      >
      My code, inherited from a VB6 version of an app that ran under W98,
      loads an image from a file into a PictureBox. The user may want to
      move or delete the file that is being shown. The corresponding event-
      driven code for "delete this picture" uses:
      >
             My.Computer.Fil eSystem.DeleteF ile(sPath)
      >
      The exception which is thrown tells me that the system cannot access
      the file as it's "being used by another process". What other process?
      (There is no multithreading in the app.) Presumably this means that
      you can't do anything with a jpg image file while it's being displayed
      in a PictureBox (whereas you could with VB6/W98). Annoying, but I
      decided on a workaround that meant blanking out the PictureBox before
      attempting to delete the file. I've experimented with several methods,
      including loading a new image from a different (dummy) jpg file,
      setting the Image property to a bitmap created on the fly, or just
      doing this before the delete:
      >
             frmMainForm.pic Preview.Image.D ispose()
      >
      Still no luck - or rather, sometimes the deletion takes place without
      problems, but most of the time it throws an exception, and if you loop
      around and try again and again for a minute or two, usually the
      exceptions just keep on coming. Still more mysteriously, tests suggest
      that the system finally frees up the image file for deletion after a
      few seconds in some cases (counting from where the PictureBox is
      loaded with another image), or after a few minutes in others.
      >
      Can anyone figure out what's going on?
      >
      (I know that in theory you could dispense with the PictureBox
      completely and just display the image using GDI+ graphics methods on
      the form surface, but there are various reasons why implementing such
      a solution would be hugely time-consuming for this app.)- Hide quoted text -
      >
      - Show quoted text -

      Comment

      • gerardianlewis@yahoo.com

        #4
        Re: PictureBox &quot;locks&quo t; file, cannot move/delete

        For the sake of anyone who might be looking for an answer, here's the
        latest fix. The routine below is used to load a clone of the original
        image into the picture box. Once this is done, the original image file
        can be moved, deleted, or whatever. (I have a variant of the following
        for thumbnail images, using ScaleTransform, which seems to work fine
        too).


        Public Sub LoadImageClone( ByVal sPath As String, ByVal picPicBox
        As PictureBox)
        Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox
        Dim bmpOriginal As System.Drawing. Image =
        System.Drawing. Image.FromFile( sPath) 'original file's bitmap, original
        size

        bmpClone = New Bitmap(bmpOrigi nal.Width, bmpOriginal.Hei ght)
        'create clone, initially empty, same size

        Dim gr As Graphics = Graphics.FromIm age(bmpClone) 'get object
        representing clone's currently empty drawing surface
        gr.SmoothingMod e = Drawing2D.Smoot hingMode.None
        gr.Interpolatio nMode =
        Drawing2D.Inter polationMode.Ne arestNeighbor
        gr.PixelOffsetM ode = Drawing2D.Pixel OffsetMode.High Speed
        gr.DrawImage(bm pOriginal, 0, 0, bmpOriginal.Wid th,
        bmpOriginal.Hei ght) 'copy original image onto this surface
        gr.Dispose()

        bmpOriginal.Dis pose()

        picPicBox.Image = bmpClone 'assign the clone to picture box

        End Sub

        Comment

        • kevininstructor@state.or.us

          #5
          Re: PictureBox &quot;locks&quo t; file, cannot move/delete

          Thanks for sharing, works for me.

          <gerardianlewis @yahoo.comwrote in message
          news:4beebe58-f18f-4876-85ee-714c2d993bcf@i3 6g2000prf.googl egroups.com...
          For the sake of anyone who might be looking for an answer, here's the
          latest fix. The routine below is used to load a clone of the original
          image into the picture box. Once this is done, the original image file
          can be moved, deleted, or whatever. (I have a variant of the following
          for thumbnail images, using ScaleTransform, which seems to work fine
          too).
          >
          >
          Public Sub LoadImageClone( ByVal sPath As String, ByVal picPicBox
          As PictureBox)
          Dim bmpClone As Bitmap 'the clone to be loaded to a PictureBox
          Dim bmpOriginal As System.Drawing. Image =
          System.Drawing. Image.FromFile( sPath) 'original file's bitmap, original
          size
          >
          bmpClone = New Bitmap(bmpOrigi nal.Width, bmpOriginal.Hei ght)
          'create clone, initially empty, same size
          >
          Dim gr As Graphics = Graphics.FromIm age(bmpClone) 'get object
          representing clone's currently empty drawing surface
          gr.SmoothingMod e = Drawing2D.Smoot hingMode.None
          gr.Interpolatio nMode =
          Drawing2D.Inter polationMode.Ne arestNeighbor
          gr.PixelOffsetM ode = Drawing2D.Pixel OffsetMode.High Speed
          gr.DrawImage(bm pOriginal, 0, 0, bmpOriginal.Wid th,
          bmpOriginal.Hei ght) 'copy original image onto this surface
          gr.Dispose()
          >
          bmpOriginal.Dis pose()
          >
          picPicBox.Image = bmpClone 'assign the clone to picture box
          >
          End Sub

          Comment

          Working...