Solved

image being used by another process

Posted on 2013-01-30
5
476 Views
Last Modified: 2013-01-30
I have a process for pulling an image from the database when I use it once it pulls fine if I close the form in the app and open it again I ge tthe same error

any ideas
        private void ShowImage(int ID)
        {
            int @id = ID;
            string conString = ConfigurationManager.ConnectionStrings["connection"].ToString();
            SqlConnection conn = new SqlConnection(conString);
            SqlDataAdapter da = new SqlDataAdapter("spx_MsgUploadFiles_Pull", conn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("@id", id);
            DataSet ds = new DataSet("read");
            conn.Open();
            da.Fill(ds, "read");
            conn.Close();
            //storing the file in byte array 
            byte[] MyData = new byte[0];
            DataRow myRow;
            myRow = ds.Tables["read"].Rows[0];
            MyData = (byte[])myRow["Document_File"];
            
            string name = myRow["filename"].ToString();
            int ArraySize = new int();
            ArraySize = MyData.GetUpperBound(0);
            if (ArraySize > -1)
            {

                string temp = ConfigurationManager.AppSettings["AppfileLogRootPath"];
                string path = temp + name;
                File.WriteAllBytes(path, MyData);
                byte[] pdf = (byte[])myRow["Document_File"];
                Image image = Image.FromFile(path);
                pbImage.Image = image;
            }

        }

Open in new window

0
Comment
Question by:r3nder
  • 2
  • 2
5 Comments
 
LVL 6

Author Comment

by:r3nder
ID: 38835969
exact error
The process cannot access the file '\\server\Users\Logs\newuser\openfiles\image.jpg' because it is being used by another process.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 38836052
Change from ImageFromFile():

            Image image = Image.FromFile(path);
            pbImage.Image = image;

To Image.FromStream():

            Image image;
            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
            {
                image = Image.FromStream(fs);
            }
            pbImage.Image = image;

See Image.FromFile():
http://msdn.microsoft.com/en-us/library/stf701f5.aspx

    "The file remains locked until the Image is disposed."
0
 
LVL 27

Expert Comment

by:nmarun
ID: 38836067
Or you can try something and dispose it off correctly.

      public partial class Form1 : Form
      {
          private Image _image;

            public Form1()
            {
                  InitializeComponent();
                _image = Image.FromFile(@"c:\temp\image.jpg");
                pictureBox1.Image = _image;
            }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_image != null)
            {
                _image.Dispose();
            }
        }
      }
0
 
LVL 27

Expert Comment

by:nmarun
ID: 38836074
Just use your own code to show the image, just that while closing the form, you explicitly call the Dispose() method on the image instance. This way your code is doing all the clean up before it closes.

Arun
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38836116
as always Idle, you are the Batman of the code world :o)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2010 Build fails to install 14 75
Java array 10 55
Need help with another query 10 34
C# MDI - Use Button on Child Form to Open (or Show) a Different Child Form 3 14
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

685 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