Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Image.FromFile is locking the file

Posted on 2002-07-27
9
Medium Priority
?
704 Views
Last Modified: 2007-12-19
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!=System.Data.ConnectionState.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)lstImages[pID]);
                              lstImages.Remove(pID);
                         }
                         imlImages.Images.Add(Image.FromHbitmap(FromFile("TMP.ICO"));
                         lstImages.Add(dr["ImageID"], (int) imlImages.Images.Count);
                    }
               }
               finally
               {
                    dr.Close();
               }

               if(Connection.State!=System.Data.ConnectionState.Closed)
                    Connection.Close();
               
               return(true);
          }
0
Comment
Question by:mAmitai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7182079
imlImages.Images.Add(Image.FromHbitmap(FromFile("TMP.ICO"));


The above call locked the file. You need to dispose the image object that is loading this image file.

Image myImage = Image.FromHbitmap(FromFile("TMP.ICO"));
imlImages.Images.Add(myImage);
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.
0
 
LVL 1

Author Comment

by:mAmitai
ID: 7182157
I tried it it dosen't work.

P.S.
Sould be:
imlImages.Images.Add(Image.FromFile("TMP.ICO"));
0
 
LVL 1

Author Comment

by:mAmitai
ID: 7182619
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.

****!!!
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 1

Author Comment

by:mAmitai
ID: 7183081
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\\Wallpaper\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7183400
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.
0
 
LVL 1

Author Comment

by:mAmitai
ID: 7183724
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\\Wallpaper\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();


I will accept it so others can use the answer as well.

  Amitai.
0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 200 total points
ID: 7183818
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\\Wallpaper\\Fly Away.jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
0
 
LVL 1

Author Comment

by:mAmitai
ID: 7184468
Resolution: (Q311754)(MSKB)
0
 
LVL 23

Expert Comment

by:naveenkohli
ID: 7184885
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question