Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:47:47+00:00 2026-05-20T04:47:47+00:00

Help! I can’t figure out how to close the file. It always gives me

  • 0

Help! I can’t figure out how to close the file. It always gives me IOException file, something like it is being used by another process

Here’s my code

private void uploadpic_btn_Click(object sender, EventArgs e)
{
    open_dialog = new OpenFileDialog();

    open_dialog.Title = "Open picture";
    open_dialog.Filter = "JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg";

    if (open_dialog.ShowDialog() != DialogResult.Cancel)
    {
        uploadpic_pb.BackgroundImage = Image.FromFile(open_dialog.FileName);
        uploadpic_pb.BackgroundImageLayout = ImageLayout.Stretch;
        uploadpic_pb.BorderStyle = BorderStyle.FixedSingle;
    }
}

private void saveBTN_Click(object sender, EventArgs e)
{
    string targetPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "\\Pictures");
    string destFile = Path.Combine(targetPath, "Copied.jpg");

    if (!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }

    File.Copy(open_dialog.FileName, destFile, true);
}

private void Form1_Load(object sender, EventArgs e)
{
    Image myimage = Image.FromFile(@"C:\Pictures\Copied.jpg");

    uploadpic_pb.BackgroundImage = myimage;
    uploadpic_pb.BackgroundImageLayout = ImageLayout.Stretch;
    uploadpic_pb.BorderStyle = BorderStyle.FixedSingle;
}

The exception returns

The process cannot access the file ‘C:\Pictures\Copied.jpg’ because it is being used by another process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:47:48+00:00Added an answer on May 20, 2026 at 4:47 am

    Image.FromFile() puts a write lock on the file. Form1_Load() thus puts a lock on Copied.jpg. You then press the uploadpic_btn button to assign a new bitmap to the BackgroundImage property. Next pressing saveBTN is however likely to fail the way you’ve written the code. Copied.jpg is still locked, the Image object still exists. It doesn’t disappear until the garbage collector runs.

    To avoid waiting for this, you’ll have to dispose the image. Fix:

        if (open_dialog.ShowDialog() != DialogResult.Cancel)
        {
            if (uploadpic_pb.BackgroundImage != null) uploadpic_pb.BackgroundImage.Dispose();
            uploadpic_pb.BackgroundImage = Image.FromFile(open_dialog.FileName);
            // etc...
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully somebody can help me out here. I'm working within an embedded ActionScript2 and
Yes, no? How can I achieve something like this: In Client side loop do
someone can help me with code? How to search in text file any word
Hopefully someone can help me out on this one. Consider the following use-cases: Add
Anybody can help me? When calling HttpResponse response = client.execute(request); It throws an IOException
Someone can help me write a small ssh script to test if file /usr/modified.txt
Anyone can help me with how to concatenate static text with databinding value like
maybe anyone can help me out with my SELECT statement on MS SQL Server.
Anyone can help ? I am having trouble, already unzipped the file I downloaded
Who can help me to fix the following problem? Here is the issue: in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.