image being used by another process

Posted on 2013-01-30
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");
            da.Fill(ds, "read");
            //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

Question by:r3nder
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
  • 2
  • 2

Author Comment

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.
LVL 86

Accepted Solution

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():

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

Expert Comment

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

      public partial class Form1 : Form
          private Image _image;

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

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            if (_image != null)
LVL 27

Expert Comment

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.


Author Closing Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to find a web event on the IIS server 4 54
I need help parsing JSON in my MVC.Net Unit Test project 1 13
junit initializtion error 2 22
Example code 13 43
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

738 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