2

Link to my previous question, which gives background to the situation

I answered my own question above by programmatically changing images from the tempory folder where attachments are saved. This caused a new issue for me when fixing my own problem that I feel is too separate from the former.

As my program closes, I delete the images from the temporary directory. Since I have got the preview to work fine upon clicking on the different images. I get the following error when trying to close the program (the deleting of images happens on this event):

The process cannot access the file 'c:\temp\DigitalArchive\FILENAME.jpg' because it is being used by another process.

So I have attempted to resolve it by clearing the picture from the temp folder before hand via:

if (picAttachPreview.Image != null)
        {
            picAttachPreview.Image.Dispose();
            picAttachPreview.Refresh();
        }

        //Runs through each file in the temporary directory and removes them to clear folder
        foreach (string item in Directory.GetFiles(tempfolder))
        {
            File.Delete(item);
        }

Edit: I feel I should show where the image is updated for reference:

if (chkAttachments.Text.Contains(".jpg"))
        {
            var selectedImage = chkAttachments.Text;
            picAttachPreview.Image = Image.FromFile(Path.Combine(tempfolder, selectedImage));
        }
KayleighArianna
  • 159
  • 1
  • 10
  • It seems you are calling `Dispose` only on that single picture that was selected when closing the app. I guess you need to dispose all the images you loaded from that folder. – René Vogt Aug 08 '16 at 12:31
  • +1. You need to make sure you dispose every image you ever showed in that picture box or otherwise loaded. – PMF Aug 08 '16 at 12:33
  • 1
    See [here](http://stackoverflow.com/questions/37736815/overwrite-image-picturebox-in-c-sharp/37741101?s=1|5.7277#37741101) for one of dozens of post about this error! – TaW Aug 08 '16 at 12:44
  • @PMF I tried putting the same `Dispose()` and `Refresh()` just before the point of updating the image (in a separate part of the code) (only usable when there is actually an image in the box) but it only caused problems where no image would show :( – KayleighArianna Aug 08 '16 at 12:58
  • `Image.FromFile` keeps the file in use. You can use `Image.FromStream` instead. – Reza Aghaei Aug 08 '16 at 13:00
  • See also e.g. https://stackoverflow.com/questions/25101377/delete-file-which-was-in-picture-box-before-loading-another-picture, https://stackoverflow.com/questions/7665775/cannot-delete-file-using-c-sharp-windows-forms-application, https://stackoverflow.com/questions/10431868/unlocking-image-from-picturebox, and https://stackoverflow.com/questions/14409905/allow-user-to-delete-file-in-use-by-my-application, to name a few others. – Peter Duniho Aug 08 '16 at 17:27
  • @PeterDuniho Why should user neglect a clean answer which and follow some links are irrelevant like [proposed duplicate](http://stackoverflow.com/questions/16067859/picturebox-and-dispose) or [Your last option](http://stackoverflow.com/questions/14409905/allow-user-to-delete-file-in-use-by-my-application) or are without any vote [like your first option](http://stackoverflow.com/questions/25101377/delete-file-which-was-in-picture-box-before-loading-another-picture)? – Reza Aghaei Aug 08 '16 at 19:18
  • @PeterDuniho Or cause error [like your second oprtion](http://stackoverflow.com/questions/7665775/cannot-delete-file-using-c-sharp-windows-forms-application) – Reza Aghaei Aug 08 '16 at 19:19
  • @PeterDuniho The only option which may help the OP is the [third option](http://stackoverflow.com/questions/10431868/unlocking-image-from-picturebox) which you offered. Compare that answer with my clean answer and that question (including the solution added in question!) with this one. It's appreciated if you want to help to keep the site clean from duplicate answers, but you should provide an acceptable link as duplicate. – Reza Aghaei Aug 08 '16 at 19:24
  • @Reza: frankly, you are the one polluting the site by answering questions that have _numerous_ duplicates already (this isn't the first time I've seen you do this). There are a lot of different possible approaches to this basic issue; if you have an opinion as to what the "right" duplicate is, you should have expressed that opinion earlier by closing the question as a duplicate of that answer. As far as I'm concern, you gave up the right to complain when you perpetuated the problem of answering duplicate questions instead of voting to close. Have a nice day. – Peter Duniho Aug 08 '16 at 19:28
  • 1
    @PeterDuniho Answering questions which doesn't have obvious duplicates is something that happens for all of us. I usually use my right to close questions as duplicate when I find an exact and obvious duplicate simply. But surely when I couldn't simply find an obvious duplicate instead of wasting my time to read some answers which have no votes or reading posts which have no accepted answer or thinking about how some post may help the OP, I can post a clean answer like this. Accusing me to polluting the site is really offensive. – Reza Aghaei Aug 08 '16 at 19:40
  • @PeterDuniho Look at the linked posts that you provided and read them. Probably you have enough knowledge in windows forms area to provide a better link if you know. – Reza Aghaei Aug 08 '16 at 19:44
  • @PeterDuniho After seeing a question which has not been answered after 45 minutes and 20-30 views and 2 wrong answers, and not finding a suitable exact duplicate what would you do? I believe answering the question is the right choice. Also if a user propose a good duplicate, surely it's completely OK. – Reza Aghaei Aug 08 '16 at 19:53
  • @PeterDuniho Don't behave based on grudge. We are here to help other users not for fight. – Reza Aghaei Aug 08 '16 at 19:57
  • @Reza: this has nothing to do with a "grudge". I have none. My response here is directly related to your specific actions in this particular situation. The fact that you have a history of this behavior has nothing to do with my own closure of this question. The fact remains that there are _numerous_ identical questions on Stack Overflow. If you think you can answer better, the right thing to do is add your answer to one of the numerous duplicates. As it is, you're just adding to the problem. What do you care about more? Your reputation points or the quality of the site? Seems like the former. – Peter Duniho Aug 08 '16 at 20:22
  • @PeterDuniho I care about both of them, but I wont sacrifice the quality of site because of my reputation points. I also close questions as duplicates but I try to close more cautiously. Also in a respectful manner to both the OP and those ones who answered the question. Also if a user with High score in tag is there, I wont close the question single handedly . If I think the question is candidate to close, I'll just leave a comment or will talk with the user with high score. I'm not here to punish others. – Reza Aghaei Aug 08 '16 at 20:41
  • 1
    @PeterDuniho thank you for closing my question to more answers by marking it as "an exact duplicate" as something that a. Doesnt fix my problem and b. Is for a different situation. From reading all the rules to SO, they tell you that your questions must be specific for specific answers so i tried to explain my question in as detailed a manner as I could. You are now criticising the one person who gave me a proper answer and tried to help. I have read and attempted the solutions within the links you provided but they are to solve different problems than my own. My problem is still unsolved but – KayleighArianna Aug 08 '16 at 20:56
  • But I think Reza has taken me into a better postion to fix it. You argue that you are just trying to maintain the quality of Stack Overflow but you have rendered my unanswered question as closed on the grounds that it is an exact duplicate of a question that is not the same as mine. Did you even read my question properly? As far as I'm aware SO is a question and answer website designed to solve specific problems. The people answering arent doing it for points, they're doing it to help people. If I cannot get this question answered nor ask again... What do you suggest I do? – KayleighArianna Aug 08 '16 at 20:57
  • _"What do you suggest I do"_ -- I suggest that you, as an aspiring programmer, learn to understand that a problem need not be a character-by-character duplicate of the exact code, for it to actually be exactly the same problem, and to learn to apply knowledge found in such similar situations to your own scenarios, even when those scenarios don't contain the same verbatim code you have. – Peter Duniho Aug 08 '16 at 21:00
  • @Reza: _"I wont sacrifice the quality of site because of my reputation points"_ -- you are doing exactly that. – Peter Duniho Aug 08 '16 at 21:00

1 Answers1

4

Image.FromFile keeps the file in use. You can use Image.FromStream instead.

var file = @"d:\pic\1.jpg";
using (var s = new System.IO.FileStream(file, System.IO.FileMode.Open))
{
    pictureBox1.Image = Image.FromStream(s);
}
System.IO.File.Delete(file);
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • As a side-note to get all `*.jpg` file from a directory, you can use `var files = System.IO.Directory.GetFiles(@"d:\pic", "*.jpg");` – Reza Aghaei Aug 08 '16 at 13:46
  • Also to check file extension, you can use `if(System.IO.Path.GetExtension(file).ToLower() == ".jpg"){}` – Reza Aghaei Aug 08 '16 at 13:46