Results 1 to 3 of 3

Thread: PictureBox won't let me overwrite .bmp file

  1. #1
    gamma4 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Apr 2006
    Posts
    2
    Reputation
    0

    Unhappy PictureBox won't let me overwrite .bmp file

    Hello,

    I want to display a preview of an image on my form. I've been trying to do this with a PictureBox control, but have repeatedly run into the same problem.

    The following code will draw the image I want (FYI sublayout is a proprietary API object that stores an image layout, and prev_pcbx is a PictureBox):

    -snip-
    Dim tempImage As Image
    subLay.LayoutToFile(GenPath & "preview.bmp")
    Prev_pcbx.Image = tempImage.FromFile(GenPath & "preview.bmp")
    -snip-

    However, when I run the same code again, I get a "preview.bmp is busy" message when subLay.LayoutToFile is called. I tried deleting the .bmp before this call, but then I get a message saying that the file is being used by another thread.

    What can I do to remedy this issue?

    Thanks in advance.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,364
    Reputation
    1544
    The documentation provided by Microsoft is really very useful. Here's a quote from the help topic for the Image.FromFile method:
    The file remains locked until the Image object is disposed.
    This means that you must either call Dispose on the Image object to release the file, which destroys the Image so it can not be used anymore, or else not lock the file in the first place by loading the image a different way. The .NET 2.0 PictureBox has a Load method that takes care of that but in previous versions this is your best bet:
    Code:
            Dim file As New IO.FileStream("file path here", IO.FileMode.Open)
    
            Me.PictureBox1.Image = Image.FromStream(file)
            file.Close()

  3. #3
    gamma4 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 1.1 (VS 2003)
    Join Date
    Apr 2006
    Posts
    2
    Reputation
    0

    Thanks

    Thanks a lot.

    Your filestream suggestion worked great.

    I tried disposing of the Image object last night, but the Dispose method kept failing. I don't think I really understand the Image object well.

    Again, thank for your help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking