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 6181647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:03:47+00:00 2026-05-24T01:03:47+00:00

I’m trying to write some code that deletes an image off the hard-disk once

  • 0

I’m trying to write some code that deletes an image off the hard-disk once the user clicks on some delete button. Sometimes I get the following exception and sometimes I do not. And when I actually do, if I try to delete it again, it does work most of the time.

This is the exception:

System.IO.IOException: The process cannot access the file because it
is being used by another process.

I guess I should provide some details on what is happening exactly:

  • User uploads an image, which is then displayed on the screen so the user can see what he/she has just uploaded.
  • A delete button is shown to the user in case he/she decides that they do not really want to upload this image.
  • When the user clicks the delete button, I call a method that deletes the image and all of its previously created thumbs.
  • Finally, the image is remove from the screen and the user can upload other images.

I’m not sure how I could solve this problem because the exception does not provide any information about which other process is holding onto the file. Any ideas?

UPDATE:

    public byte[] ResizeImageToBytes(string path, int size, string name)
    {
        var newImage = Image.FromFile(path);
        int newWidth; int newHeight;
        if (size == 470)
        {
            if (newImage.Height != 250)
            {
                newWidth = (int)Math.Round(newImage.Width * (100 / (newImage.Height / 250)) * 0.01);
                newHeight = 250;
            }
            else
            {
                newWidth = newImage.Width;
                newHeight = newImage.Height;
            }
        }
        else
        {
            if (newImage.Width > newImage.Height)
            {
                newWidth = size;
                newHeight = newImage.Height*size/newImage.Width;
            }
            else
            {
                newWidth = newImage.Width*size/newImage.Height;
                newHeight = size;
            }
        }

        var thumb = new Bitmap(newWidth, newHeight);
        var gfx = Graphics.FromImage(thumb);
        gfx.CompositingQuality = CompositingQuality.HighQuality;
        gfx.SmoothingMode = SmoothingMode.HighQuality;
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

        var rect = new Rectangle(0, 0, newWidth, newHeight);
        gfx.DrawImage(newImage, rect);
        var ms = new MemoryStream();
        thumb.Save(ms, newImage.RawFormat);
        return ms.GetBuffer();
    }

    public void SaveImage(byte[] toSave, string path)
    {
        using (var ms = new MemoryStream())
        {
            ms.Write(toSave, 0, toSave.Length);
            using(var theImage = Image.FromStream(ms)) 
            {
                theImage.Save(path);
            }
        }
    }

    [HttpPost]
    public ActionResult Upload()
    {
        var newFile = System.Web.HttpContext.Current.Request.Files["Filedata"];
        string guid = Guid.NewGuid() + newFile.FileName;
        string itemImagesFolder = Server.MapPath(Url.Content("~/Content/ItemImages/"));
        string fileName = itemImagesFolder + "originals/" + guid;
        newFile.SaveAs(fileName);

        string finalPath;
        foreach (var dim in ImageDimensionsList.Options)
        {
            var bytes = _imageService.ResizeImageToBytes(fileName, dim.Width, guid);
            finalPath = itemImagesFolder + dim.Title + "/" + guid;
            _imageService.SaveImage(bytes, finalPath);
        }
        return Content(guid);
    }
  • 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-24T01:03:47+00:00Added an answer on May 24, 2026 at 1:03 am

    You aren’t disposing any of the disposable resources you are working with in your ResizeImageToBytes method. This leaves leaking handles in your application and of course locked files. Try this:

    public byte[] ResizeImageToBytes(string path, int size, string name)
    {
        using (var newImage = Image.FromFile(path))
        {
            int newWidth; int newHeight;
            if (size == 470)
            {
                if (newImage.Height != 250)
                {
                    newWidth = (int)Math.Round(newImage.Width * (100 / (newImage.Height / 250)) * 0.01);
                    newHeight = 250;
                }
                else
                {
                    newWidth = newImage.Width;
                    newHeight = newImage.Height;
                }
            }
            else
            {
                if (newImage.Width > newImage.Height)
                {
                    newWidth = size;
                    newHeight = newImage.Height * size / newImage.Width;
                }
                else
                {
                    newWidth = newImage.Width * size / newImage.Height;
                    newHeight = size;
                }
            }
    
            using (var thumb = new Bitmap(newWidth, newHeight))
            using (var gfx = Graphics.FromImage(thumb))
            {
                gfx.CompositingQuality = CompositingQuality.HighQuality;
                gfx.SmoothingMode = SmoothingMode.HighQuality;
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
                var rect = new Rectangle(0, 0, newWidth, newHeight);
                gfx.DrawImage(newImage, rect);
                using (var ms = new MemoryStream())
                {
                    thumb.Save(ms, newImage.RawFormat);
                    return ms.GetBuffer();
                }
            }
        }
    }
    

    As far as your SaveImage method is concerned, well, this method seems redundant to me as it already exists in the .NET framework. It’s called File.WriteAllBytes:

    public void SaveImage(byte[] toSave, string path)
    {
        File.WriteAllBytes(path, toSave);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.