mAmitai
asked on
Image.FromFile is locking the file
Hi
Hi have this problem.
I am loading images from Sql2000 to file and from the file to an ImageList.
1. Can I load the images directly to the imageList. (Else)
2. When the data is saved to a Temp file and then load to the ImageList, The temp file stay locked, so, when the next image is ready to be loaded, I am getting an error:
~"File is in use (or locked) by another process".
Help!!!
Amitai
public bool ImageLoad(int pID)
{
SqlCommand cmd = new SqlCommand("EXEC dbo.spGOPImages_Get " + pID.ToString(), Connection);
long Size, Start=0;
int BuffSize=2048;
int ImageData = 4;
byte[] Buff = new byte[BuffSize-1];
if(Connection.State!=Syste m.Data.Con nectionSta te.Open)
Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
try
{
while (dr.Read())
{
FileStream fs = new FileStream("TMP.ICO", FileMode.OpenOrCreate, FileAccess.Write);
Start = 0;
if (!dr.IsDBNull(ImageData))
{
Size = dr.GetBytes(ImageData, Start, Buff, 0, BuffSize);
while(Size > 0 && Size < BuffSize)
{
fs.Write(Buff, 1, (int) Size);
fs.Flush();
Start=Start+BuffSize;
Size=dr.GetBytes(ImageData , Start, Buff, 0, BuffSize);
}
}
fs.Close();
fs = null;
if(lstImages[pID] != null)
{
imlImages.Images.RemoveAt( (int)lstIm ages[pID]) ;
lstImages.Remove(pID);
}
imlImages.Images.Add(Image .FromHbitm ap(FromFil e("TMP.ICO "));
lstImages.Add(dr["ImageID" ], (int) imlImages.Images.Count);
}
}
finally
{
dr.Close();
}
if(Connection.State!=Syste m.Data.Con nectionSta te.Closed)
Connection.Close();
return(true);
}
Hi have this problem.
I am loading images from Sql2000 to file and from the file to an ImageList.
1. Can I load the images directly to the imageList. (Else)
2. When the data is saved to a Temp file and then load to the ImageList, The temp file stay locked, so, when the next image is ready to be loaded, I am getting an error:
~"File is in use (or locked) by another process".
Help!!!
Amitai
public bool ImageLoad(int pID)
{
SqlCommand cmd = new SqlCommand("EXEC dbo.spGOPImages_Get " + pID.ToString(), Connection);
long Size, Start=0;
int BuffSize=2048;
int ImageData = 4;
byte[] Buff = new byte[BuffSize-1];
if(Connection.State!=Syste
Connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
try
{
while (dr.Read())
{
FileStream fs = new FileStream("TMP.ICO", FileMode.OpenOrCreate, FileAccess.Write);
Start = 0;
if (!dr.IsDBNull(ImageData))
{
Size = dr.GetBytes(ImageData, Start, Buff, 0, BuffSize);
while(Size > 0 && Size < BuffSize)
{
fs.Write(Buff, 1, (int) Size);
fs.Flush();
Start=Start+BuffSize;
Size=dr.GetBytes(ImageData
}
}
fs.Close();
fs = null;
if(lstImages[pID] != null)
{
imlImages.Images.RemoveAt(
lstImages.Remove(pID);
}
imlImages.Images.Add(Image
lstImages.Add(dr["ImageID"
}
}
finally
{
dr.Close();
}
if(Connection.State!=Syste
Connection.Close();
return(true);
}
ASKER
I tried it it dosen't work.
P.S.
Sould be:
imlImages.Images.Add(Image .FromFile( "TMP.ICO") );
P.S.
Sould be:
imlImages.Images.Add(Image
ASKER
Hello navee.
Defenatly dosen't work! I checked again.
When i Dispose the image the image in the ImageList allso distroied. Even image.Clone is not working.
****!!!
Defenatly dosen't work! I checked again.
When i Dispose the image the image in the ImageList allso distroied. Even image.Clone is not working.
****!!!
ASKER
I founs an MS Article about that problem...
Q311754
However it creates problems when I try to use the image
I am getting an error:
pictureBox1.Image=(Image) this.imlImages.Images[1];
-->"A generic error occurred in GDI+"
???????????????????????
Resolution: (Q311754)
To work around this problem, use the FileStream object as follows:
// Make sure that you have added the System.IO namespace.
using System.IO;
// Specify a valid picture file path on your computer.
FileStream fs;
fs = new FileStream("C:\\WINNT\\Web \\Wallpape r\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromS tream(fs);
fs.Close();
Q311754
However it creates problems when I try to use the image
I am getting an error:
pictureBox1.Image=(Image) this.imlImages.Images[1];
-->"A generic error occurred in GDI+"
???????????????????????
Resolution: (Q311754)
To work around this problem, use the FileStream object as follows:
// Make sure that you have added the System.IO namespace.
using System.IO;
// Specify a valid picture file path on your computer.
FileStream fs;
fs = new FileStream("C:\\WINNT\\Web
pictureBox1.Image = System.Drawing.Image.FromS
fs.Close();
put an exception handler around
pictureBox1.Image=(Image) this.imlImages.Images[1];
and check the call stack and see where it is failing inside framework. And if possible get some more information about the exception.
pictureBox1.Image=(Image) this.imlImages.Images[1];
and check the call stack and see where it is failing inside framework. And if possible get some more information about the exception.
ASKER
Well it was somthing complitly different
The MS Q311754 Solved it.
Since I can't give the points to myself and you Naveenkoli were the only one who bothered I will give the points to you. I sugest that you put the resolution:
Resolution: (Q311754)
To work around this problem, use the FileStream object as follows:
// Make sure that you have added the System.IO namespace.
using System.IO;
// Specify a valid picture file path on your computer.
FileStream fs;
fs = new FileStream("C:\\WINNT\\Web \\Wallpape r\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromS tream(fs);
fs.Close();
I will accept it so others can use the answer as well.
Amitai.
The MS Q311754 Solved it.
Since I can't give the points to myself and you Naveenkoli were the only one who bothered I will give the points to you. I sugest that you put the resolution:
Resolution: (Q311754)
To work around this problem, use the FileStream object as follows:
// Make sure that you have added the System.IO namespace.
using System.IO;
// Specify a valid picture file path on your computer.
FileStream fs;
fs = new FileStream("C:\\WINNT\\Web
pictureBox1.Image = System.Drawing.Image.FromS
fs.Close();
I will accept it so others can use the answer as well.
Amitai.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Resolution: (Q311754)(MSKB)
To avoid this problem, i would suggest that you use Guids to name your temp files and implement a file watcher on tyhat folder that will clean up the files every X number of hours/days/months.
This is the technique we use allthe time and avoid headaches of exclusve file locking.
This is the technique we use allthe time and avoid headaches of exclusve file locking.
The above call locked the file. You need to dispose the image object that is loading this image file.
Image myImage = Image.FromHbitmap(FromFile
imlImages.Images.Add(myIma
myImage.Dispose();
The other problem you are going to run into is that since you are using the same file for saving the image, if simultaneous calls are made into this method, the file will be locked for one. You may need to think about synchrnizing the call so that if one thread is using it, other has to wait.