|
-
Aug 17th, 2005, 04:02 PM
#1
Thread Starter
Fanatic Member
[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:
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 ...
-
Aug 17th, 2005, 05:44 PM
#2
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!!
-
Aug 17th, 2005, 06:01 PM
#3
Fanatic Member
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
-
Aug 17th, 2005, 06:12 PM
#4
-
Aug 17th, 2005, 06:17 PM
#5
Fanatic Member
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
-
Aug 17th, 2005, 06:35 PM
#6
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!!
-
Aug 17th, 2005, 06:42 PM
#7
Fanatic Member
Re: Image File locked
 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
-
Aug 17th, 2005, 06:49 PM
#8
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!!
-
Aug 18th, 2005, 01:03 AM
#9
Thread Starter
Fanatic Member
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:
Try
ofs = New FileStream(sLogoFile, FileMode.Open, FileAccess.Read)
oMemStream = New MemoryStream(Convert.ToInt32(ofs.Length))
oBinReader = New BinaryReader(ofs)
Dim bytesRead As Byte() = oBinReader.ReadBytes(Convert.ToInt32(ofs.Length))
oMemStream.Write(bytesRead, 0, Convert.ToInt32(ofs.Length))
' picLogo.Image.FromStream(oMemStream) ' This doesn't show the image. Why ????
picLogo.Image = New Bitmap(oMemStream)
ofs.Close()
Catch ex As Exception
Finally
ofs = Nothing ' release file
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 ...
-
Aug 18th, 2005, 06:29 AM
#10
Fanatic Member
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:
Dim bytesRead As Byte(Convert.ToInt32(ofs.Length))
ofs.Read(bytesRead, 0, Convert.ToInt32(ofs.Length))
oMemStream.Write(bytesRead, 0, Convert.ToInt32(ofs.Length))
' picLogo.Image.FromStream(oMemStream) ' This doesn't show the image. Why ????
'because it is a shared function that [b]returns[/b] an image.
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
-
Aug 18th, 2005, 07:30 AM
#11
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.
-
Aug 18th, 2005, 07:31 AM
#12
Re: Image File locked
Rep me now or later its up to you
I don't live here any more.
-
Aug 18th, 2005, 08:45 AM
#13
Thread Starter
Fanatic Member
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 ...
-
Aug 18th, 2005, 12:16 PM
#14
Re: Image File locked
 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!!
-
Aug 18th, 2005, 04:06 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|