Results 1 to 15 of 15

Thread: [RESOLVED] Image File locked

  1. #1

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    [RESOLVED] Image File locked

    Hi

    I am curious to find out why an image file is locked when I load it in a picturebox or Image variable using the code below:
    VB Code:
    1. picImage.Image = Image.FromFile("abc.tif")

    I can work around the locking bit by reading the image file in a memory stream and then load the Image from the memory stream but I was wondering if there was an alternative way to just assign the filename to the picturebox and not having the image file locked.

    Thanks
    Last edited by Mr.No; Aug 18th, 2005 at 09:04 AM. Reason: Issue resolved
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

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

    Re: Image File locked

    no
    I've had trouble with this before. Someone tell me if its still the same in .NET 2.0


    if you read on MSDN, it says that when you load the image, you are responsible to leave the image filestream open until the image is dispose. That means that the file remains locked until you're done using the image. It's a quite annoying thing I'd say. I think when I wanted to get around this I would clone the image, I cant remember quite clearly.
    your code is ok, its .NET
    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!!

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

    Re: Image File locked

    It would not be much code to just open a stream yourself and say: Image.FromStream and then close the stream

    Turns out I'm wrong, the stream must remain open
    Last edited by grilkip; Aug 17th, 2005 at 06:13 PM.
    "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
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Image File locked

    thats exactly what I just said above
    arent you just ashamed of yourself
    it works fine if you do what he said and duplicate the image. The problem is, it's redundant. If you're dealing with a lot of images, then you're talking about a big loss of performance/memory etc etc
    anyone can check this in .net 2.0?
    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!!

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

    Re: Image File locked

    I only just took you of my ignore list MrPolite

    I wonder when you do what Mr No said and load it into memory first and use that stream, would the new Image Object 'encapsulate' the data or would it copy all of the imagedata to a new location?
    "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

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

    Re: Image File locked

    well it has to make a copy somewhere
    I'm not sure what he's doing exactly. The memory stream would need to copy the image data to memory, so I think no matter what you do, you'd end up with two copies of the image
    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!!

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

    Re: Image File locked

    Quote Originally Posted by MrPolite
    well it has to make a copy somewhere
    I'm not sure what he's doing exactly. The memory stream would need to copy the image data to memory, so I think no matter what you do, you'd end up with two copies of the image
    If you create an MemoryStream from an image, you are making the copy (from the disk). I was wondering if when you create an image from a MemoryStream is the data from that stream being copied again or is it 'used' directly.

    I don't know if I'm making sence, I can hardly follow my own thoughts.
    "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

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

    Re: Image File locked

    makes sense
    I believe it'd be used directly. However.... .it seems like you can't load a file into a memory stream directly (or am I wrong?) so I think you'd have to first load the file, then make a copy of it in the memory stream
    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!!

  9. #9

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: Image File locked

    Thanks for the responses guys. Here's the code I currently use, I had a clue from this link Stream
    VB Code:
    1. Try
    2.       ofs = New FileStream(sLogoFile, FileMode.Open, FileAccess.Read)
    3.       oMemStream = New MemoryStream(Convert.ToInt32(ofs.Length))
    4.       oBinReader = New BinaryReader(ofs)
    5.       Dim bytesRead As Byte() = oBinReader.ReadBytes(Convert.ToInt32(ofs.Length))
    6.       oMemStream.Write(bytesRead, 0, Convert.ToInt32(ofs.Length))
    7.       ' picLogo.Image.FromStream(oMemStream) ' This doesn't show the image. Why ????
    8.       picLogo.Image = New Bitmap(oMemStream)
    9.  
    10.       ofs.Close()
    11.  
    12.     Catch ex As Exception
    13.     Finally
    14.       ofs = Nothing  ' release file
    15.     End Try

    As you can see, its quite a long step just to show an image, I wonder why the PictureBox doesn't have a method for this. Mind you, while I'm typing this it suddenly occurred to me that I could SubClass the PictureBox to expose a method that would be called say ShowImageWithoutLocking. What do you think? Any alternatives?

    Thanks
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

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

    Re: Image File locked

    I'm not sure that this is more efficient because you are still buffering the whole image in a byte-array, you might as well clone it I think. And you don't really need a BinaryReader to do that
    VB Code:
    1. Dim bytesRead As Byte(Convert.ToInt32(ofs.Length))
    2.       ofs.Read(bytesRead, 0, Convert.ToInt32(ofs.Length))
    3.       oMemStream.Write(bytesRead, 0, Convert.ToInt32(ofs.Length))
    4.       ' picLogo.Image.FromStream(oMemStream) ' This doesn't show the image. Why ????
    5.       'because it is a shared function that [b]returns[/b] an image.
    6.       picLogo.Image = Image.FromStream(oMemStream)
    I typed it freehand, so forgive me small errors.
    "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

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Image File locked

    How about...

    Code:
    picturebox1.image = new Bitmap(Image.FromFile(path))
    That doesn't lock the file.
    Last edited by wossname; Aug 18th, 2005 at 07:36 AM.
    I don't live here any more.

  12. #12
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Image File locked

    Rep me now or later its up to you
    I don't live here any more.

  13. #13

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: Image File locked

    Wonderful it works.

    Thanks
    Using VB.NET 2003/.NET 1.1/C# 2.0
    http://del.icio.us/rajoo
    Blow your mind, smoke gunpowder
    Ashes to ashes, dust to dust
    If God won't have you, the devil will. - Author unknown
    Don't follow me, I'm lost too ...

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

    Re: Image File locked

    Quote Originally Posted by wossname
    How about...

    Code:
    picturebox1.image = new Bitmap(Image.FromFile(path))
    That doesn't lock the file.
    you dork
    that's what I said I've been doing (cloning the image). It's not any more efficient, as you'd be creating a copy of the image anyways
    I don't think there is a way to avoid creating a duplicate
    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!!

  15. #15
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Image File locked

    It's not cloning the image you retard. Cloning would lock the file. This is just a straight blit from one bound image to an unbound one.

    Maybe it not any faster but it takes up hardly any code and its easy to understand.
    Last edited by wossname; Aug 18th, 2005 at 04:09 PM.
    I don't live here any more.

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