Filestream Error...

T

Tim

Hi,

I have a list of products in with images stored in a SQL Server 2000 DB. I
need to be able to retrieve the images when a product is chose. I use the
code below to get the image from the DB and assign it to the picture box.

It works fine the first time but subsequent times it gives me an error
saying that the file is in use by another process. What process? If I go to
the folder and try to delete the file in windows I get the same error. Can
anyone help me here? What am I doing wrong? I am closing the filestream so
what is still open?

Thanks

Tim

try

{

//Look Up the Image

SqlCommand cmdSelect=new SqlCommand("SELECT logo_image FROM
t_store_information WHERE store_id =1" , DBConnection.DBConn.Connection);

DBConnection.DBConn.Connection.Open();

byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();

DBConnection.DBConn.Connection.Close();

string strfn=Convert.ToString(DateTime.Now.ToFileTime());

FileStream fs = new FileStream(@"C:\Program Files\MM\Temp\thumb.jpg",
FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(barrImg,0,barrImg.Length);

fs.Close();

strfn = "";

this.pboxThumb.Image = Image.FromFile(@"C:\Program
Files\MM\Temp\thumb.jpg");

}

catch(Exception ex)

{

#if DEBUG

MessageBox.Show("Image Error!\r\n\r\n" + ex.Message);

#endif

}

finally

{




DBConnection.DBConn.Connection.Close();

}
 
J

Jan

Hi Tim,

If I were you I wouldn't use a FileStream but a MemoryStream. That way
you avoid all those stick file issues and it will work a goof deal
faster for you.

For the record what might be happeneing is that the FromFile functions
implementation may not be releasnig the unmanaged FileHandle fast
enough but i can't be sure.

If you want to see what process is holding the file handle try using
Process Explorer from www.sysinternals.com.

Good Luck,
Jan
 
T

Tim

Thanks Jan,

I don't suppose you have an example of MemoryStream do you? Also I thought
that pictureboxes had to have a file that actually existed on the hard
drive.

Tim
 
J

Jan Bannister

Hi Tim,

Here's an example:

using (System.IO.MemoryStream mem = new
System.IO.MemoryStream(bytes,false))
{
System.Drawing.Bitmap bitmap = new Bitmap(mem,false);
pictureBox1.Image = Bitmap;
}

And no PictureBoxes don't need a file. They just and Image (which is
the superclass of Bitmap).

Hope that helps,
Jan
 
J

Jon Skeet [C# MVP]

Tim said:
I have a list of products in with images stored in a SQL Server 2000 DB. I
need to be able to retrieve the images when a product is chose. I use the
code below to get the image from the DB and assign it to the picture box.

It works fine the first time but subsequent times it gives me an error
saying that the file is in use by another process. What process? If I go to
the folder and try to delete the file in windows I get the same error. Can
anyone help me here? What am I doing wrong? I am closing the filestream so
what is still open?

It doesn't really mean another process - it's a misleading error
message.

Although you're closing the FileStream, you're then using
Image.FromFile, which opens another FileStream and keeps it open until
the image is disposed.

Using a MemoryStream as Jan suggested should solve the problem, but you
shouldn't use a using block - the image "owns" the stream you pass into
it, and expects you to leave it open. From the Bitmap constructor docs:

<quote>
Remarks
You must keep the stream open for the lifetime of the Bitmap object.
</quote>

You should, however, definitely use a using statement for your database
connection :)
 
T

Tim

Thanks for your reply Jon, I really appreciated it. What do you mean by "You
should, however, definitely use a using statement for your database
connection"?

Thanks

Tim
 
T

Tim

Wow, Jan thank you. I have been struggling with this problem for a while and
this works like a charm.

Tim
 
J

Jon Skeet [C# MVP]

Tim said:
Thanks for your reply Jon, I really appreciated it. What do you mean by "You
should, however, definitely use a using statement for your database
connection"?

You're currently manually calling Close on your database connection,
which means that if an exception is thrown, you're failing to close it.
(You were doing the same with the FileStream too.)

You should use a using statement (like the one Jan used for the
MemoryStream) which is the equivalent of try/finally, with Dispose
being called in the finally block.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top