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();
} 9 1879
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
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
"Jan" <ja***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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
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
Tim <ti*@home.com> wrote: 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 :)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m... Tim <ti*@home.com> wrote: 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 :)
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Wow, Jan thank you. I have been struggling with this problem for a while and
this works like a charm.
Tim
"Jan Bannister" <ja***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... 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
Tim <ti*@home.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks Jon, I have adjusted my code accordingly.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Tim <ti*@home.com> wrote: 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.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: antsays |
last post by:
I am trying to serialize a collection to disk using the code provided.
This works just fine but when I try do copy and past the xml file to
another location or sometimes even just click on the...
|
by: Patrick Sannes |
last post by:
Hi,
At this moment I have one large sourcecode file and want to seperate it into
multiple files. When I do so, the app crash after he created 6389 (156794880
bytes total) source files with the...
|
by: Dorsa |
last post by:
HI, Could you please tell me the error in here. I am trying to open an
XML file from a link.
Response.Clear()
Response.Expires = 0
Response.BufferOutput = False
Response.ContentType =...
|
by: lh |
last post by:
The following method only works when i give the ASP.net account full
permissions on the directory.
It doesn't work when i give the directory Modify, Read &Execute, List Folder
Contents, Read, and...
|
by: Nathan Sokalski |
last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
|
by: Tim_Mac |
last post by:
hi,
i'm not sure if i have chosen the best approach, but it seemed quite
good to me.
i have a collection class, containing business objects. the collection
class is static and remains in-memory...
|
by: ewingate |
last post by:
The following code which is supposed to dynamically create files with
incrementing names is throwing an exception due to the inclusion of the
DateTime.Now component of the sReportSave string:
...
|
by: =?Utf-8?B?TWlzY2hh?= |
last post by:
It is my understanding that I can create an XmlTextReader from a variety of
sources, including a stream object. I am developing a web application that
reads some data from an Xml file. Everything...
|
by: Andreas Zimmermann |
last post by:
Hi,
we just set up a SQL Server 2008 on Windows Server 2008 (configured as file
/ webserver, 16 GB memory, 4 dual core processors, 6 internal disks + 15
disc ISCSI array with overall almost 3 TB...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |