I am selecting file from openfiledialoge and displaying it in picturebox and its name in textbox when I click on delete button I am getting exception The process cannot access the file because it is being used by another process.
I searched a lot for this exception to get resolved but i didn't fine any of them working, when i tried closing file with imagename which is in textbox i.e the file i am displaying in picturebox ; using IsFileLocked method,this closes and deletes all files of particular directory path ,but how can I delete the only file shown in picturebox,where I am going wrong
public partial class RemoveAds : Form
{
OpenFileDialog ofd = null;
string path = #"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking.
public RemoveAds()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(path))
{
ofd = new OpenFileDialog();
ofd.InitialDirectory = path;
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
}
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
//Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
if (Directory.Exists(path))
{
var directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.GetFiles())
{ if(!IsFileLocked(file))
file.Delete();
}
}
}
public static Boolean IsFileLocked(FileInfo path)
{
FileStream stream = null;
try
{ //Don't change FileAccess to ReadWrite,
//because if a file is in readOnly, it fails.
stream = path.Open ( FileMode.Open, FileAccess.Read, FileShare.None );
}
catch (IOException)
{ //the file is unavailable because it is:
//still being written to or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
}
Thanks in advance for any help
The (previously) accepted answer to this question is very poor practice. If you read the documentation on System.Drawing.Bitmap, in particular for the overload that creates a bitmap from a file, you will find :
The file remains locked until the Bitmap is disposed.
in your code you create the bitmap and store it in a local variable but you never dispose of it when you are done. This means your image object has gone out of scope but has not released its lock on the image file you are trying to delete. For all objects that implement IDisposable (like Bitmap) you must dispose of them yourself. See this question for example (or search for others - this is a very important concept!).
To correct the problem properly you simply need to dispose of the image when you are done with it :
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName); // create the bitmap
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
img.Dispose(); // dispose the bitmap object
}
Please do not take the advice in the answer below - you should nearly never need to call GC.Collect and if you need to do it to make things work it should be a very strong signal that you are doing something else wrong.
Also, if you only want to delete the one file (the bitmap you have displayed) your deletion code is wrong and will delete every file in the directory as well (this is just repeating Adel's point). Further, rather than keep a global OpenFileDialog object alive simply to store the file name, I would suggest getting rid of that and saving just the file info :
FileInfo imageFileinfo; //add this
//OpenFileDialog ofd = null; Get rid of this
private void button1_Click(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(path))
{
OpenFileDialog ofd = new OpenFileDialog(); //make ofd local
ofd.InitialDirectory = path;
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);
imageFileinfo = new FileInfo(ofd.FileName); // save the file name
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
ofd.RestoreDirectory = true;
img.Dispose();
}
ofd.Dispose(); //don't forget to dispose it!
}
else
{
return;
}
}
Then in your second button handler you can just delete the one file you are interested in.
private void button2_Click(object sender, EventArgs e)
{
if (!IsFileLocked(imageFileinfo))
{
imageFileinfo.Delete();
}
}
I had the same problem : I loaded a file in a PictureBox and when trying to delete it I got the same exception.
This occurred only when the image was displayed.
I tried them all :
picSelectedPicture.Image.Dispose();
picSelectedPicture.Image = null;
picSelectedPicture.ImageLocation = null;
and still got the same exception.
Then I found this on CodeProject : [c#] delete image which is opened in picturebox.
Instead of using PictureBox.Load() it creates an Image from the file and sets it as PictureBox.Image:
...
// Create image from file and display it in the PictureBox
Image image = GetCopyImage(imagePath);
picSelectedPicture.Image = image;
...
private Image GetCopyImage(string path) {
using (Image image = Image.FromFile(path)) {
Bitmap bitmap = new Bitmap(image);
return bitmap;
}
}
No more exceptions when I delete the file.
IMHO, this is the most suitable solution.
EDIT
I forgot to mention that you can safely delete the file immediately after display :
...
// Create image from file and display it in the PictureBox
Image image = GetCopyImage(imagePath);
picSelectedPicture.Image = image;
System.IO.File.Delete(imagePath);
...
use this code
string imgName = ofd.SafeFileName;
if (Directory.Exists(path))
{
var directory = new DirectoryInfo(path);
foreach (FileInfo file in directory.GetFiles())
{
GC.Collect();
GC.WaitForPendingFinalizers();
file.Delete();
}
}
Your button2_Click event handler is cycling through all the files inside your directory & doing the deletes.
You need to change your code like the following:
public partial class RemoveAds : Form
{
OpenFileDialog ofd = null;
string path = #"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking.
string fullFilePath;
public RemoveAds()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (System.IO.Directory.Exists(path))
{
ofd = new OpenFileDialog();
ofd.InitialDirectory = path;
DialogResult dr = new DialogResult();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = new Bitmap(ofd.FileName);
string imgName = ofd.SafeFileName;
txtImageName.Text = imgName;
pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr());
fullFilePath = ofd.FilePath;
ofd.RestoreDirectory = true;
}
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo file = new FileInfo(fullFilePath);
if(!IsFileLocked(file))
file.Delete();
}
}
public static Boolean IsFileLocked(FileInfo path)
{
FileStream stream = null;
try
{ //Don't change FileAccess to ReadWrite,
//because if a file is in readOnly, it fails.
stream = path.Open ( FileMode.Open, FileAccess.Read, FileShare.None );
}
catch (IOException)
{ //the file is unavailable because it is:
//still being written to or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
}
By using GetThumnailImage you have to specify the width and height which is static.
Use the Load method instead.
eg: pictureBox1.Load(Path to the image); by using this u will have no problem in deleting the image or the folder before closing the app. no other methods need to be created.
hope this helps
Related
I am using C# and a winform and am saving data to a .xlsx on a button click event. I have a unique situation that I am not sure how to code for....
If the form is still displayed and the user clicks the button, I want it to prompt for a file name and save location. BUT if the form has not been closed and the user clicks the button a second time, I want .xlsx to be saved in the same location and with the same filename and over write with no prompt.
This is the syntax I use to prompt for save name and location, but how do I check to determine if a filename/save location has already been input and if it has do not prompt again?
private void btnOne_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = #"C:\";
save.RestoreDirectory = true;
save.Title = "Select save location file name";
save.DefaultExt = "xlsx";
if (save.ShowDialog() == DialogResult.OK)
{
try
{
var file = new FileInfo(save.FileName);
using (var package = new ExcelPackage(file))
{
package.Save();
}
}
catch { Messagebox.Show("An error has occured"; }
}
}
So, whether the data has a set filename is a part of the state of the class. Inside the class where you have btnOne_Click, just define a string with the filename, defaulted to null:
string filepath = null;
Then, in your btnOne_Click, you want to check for the filepath. If it's not there, open the saveAs dialog. After that, if filepath is set, just save. It will be restructured like this:
private void btnOne_Click(object sender, EventArgs e)
{
if (filepath == null)
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = #"C:\";
save.RestoreDirectory = true;
save.Title = "Select save location file name";
save.DefaultExt = "xlsx";
if (save.ShowDialog() == DialogResult.OK) {
filepath = save.FileName;
}
}
if (filepath != null)
{
try
{
var file = new FileInfo(filepath);
using (var package = new ExcelPackage(file))
{
package.Save();
}
}
catch { MessageBox.Show("An error has occured"; }
}
}
This logical structure gives you standard behavior for when a user presses a save button. If they cancel the saveAs dialog, then the save is aborted and the filename state is not changed.
Declare this globally:
public string Filename;
Then change your subroutine like this:
private void btnOne_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(Filename))
{
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = #"C:\";
save.RestoreDirectory = true;
save.Title = "Select save location file name";
save.DefaultExt = "xlsx";
if (save.ShowDialog() == DialogResult.OK)
{
try
{
Filename = save.FileName;
var file = new FileInfo(save.FileName);
using (var package = new ExcelPackage(file))
{
package.Save();
}
}
catch { MessageBox.Show("An error has occured"); }
}
}
else
{
var file = new FileInfo(Filename);
using (var package = new ExcelPackage(file))
{
package.Save();
}
}
}
so i made a simple project where when i click a button the picture edit get an image from a folder file, but when i want to delete the folder that contains the image, it gives me an error. the code as following
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
pictureEdit1.Image = Image.FromFile(pathx);
}
private void button2_Click(object sender, EventArgs e)
{
string dir = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage";
try {
if (Directory.Exists(dir))
{
//////give me an error in here///////
Directory.Delete(dir, true);
}
else
{
MessageBox.Show("folder not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
the purpose of this, is in my main project, for cache purpose. so i get an image from a certain folder after coping it from server to local. and when i want to close the main project i need to clear the cache or folder
Update
which is better alternate 1 or alternate 2 (to dispose)
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
//alternate1
using (FileStream stream = new FileStream(pathx, FileMode.Open, FileAccess.Read))
{
pictureEdit1.Image = Image.FromStream(stream);
//stream.Dispose();
}
//alternate2
//Image img = new Bitmap(pathx);
//pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
//img.Dispose();
}
The documentation on System.Drawing.Bitmap (http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx) says:
The file remains locked until the Bitmap is disposed.
To get around this, you should replace this line:
pictureEdit1.Image = Image.FromFile(pathx);
With this:
Image img = new Bitmap(pathx);
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
img.Dispose();
This should load the Bitmap only long enough to create a thumbnail version of the image for use in the PictureBox control, then dispose of it immediately, releasing the lock on the file but still displaying the image on the screen.
Hope this helps!
Edit: Here's the version using using that does the same thing:
using (Image img = new Bitmap(pathx)) {
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
}
I am using two windows forms, first form will have multiple image files populated in datagridview and on the second form all those images are placed in another datagridview control, the second datagridview control has a button as Delete Image that will delete the file.
after i click the button exception occurs as file is being used. but i alread have disposed the datagridview and also cleared its rows but still it is causing not to release the image. I already have tried to call GC.Collect(); Method but it didn't helped. my sample code is below.
On First form:
// Commenting the code above
if (validuser)
{
dgvImages.Rows.Clear();
dgvImages.Dispose();
GC.Collect();
var form = new NewForm();
form.Show();
this.Hide();
}
and on second form:
private void dgvImages_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 1)
{
string selectedpath = dgvImages.Rows[e.RowIndex].Cells[2].Value.ToString(); // this cell contains the full path of the image
if (File.Exists(selectedpath))
{
dgvImages.Rows.Clear();
dgvImages.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(selectedpath);
LoadImages();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); // File is in use
}
}
EDIT: Here is how i am loading images
private void LoadImages()
{
if (Directory.Exists(path))
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles();
foreach (var f in files)
{
if (f.Extension.ToLower() == ".jpg" || f.Extension.ToLower() == ".png" || f.Extension.ToLower() == ".bmp" || f.Extension.ToLower() == ".tiff")
{
dgvImages.Rows.Add((Image.FromFile(f.FullName)), "Delete Image" ,f.FullName);
}
}
}
}
By default when you use Image.FromFile it will lock the file until the image is disposed.
As per the MSDN resources
https://msdn.microsoft.com/en-us/library/4sahykhd(v=vs.110).aspx
What you want to do is load your images as per this answer here.
Open Image from file, then release lock?
Image img;
using (var bmpTemp = new Bitmap("image_file_path"))
{
img = new Bitmap(bmpTemp, true);
}
According to the new Bitmap documentation you may need to get it to use icm.
https://msdn.microsoft.com/en-us/library/3135s427(v=vs.110).aspx
Use this constructor to open images with the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF.
Image.FromFile() method lock the file by GDI+
Load image from stream using the following code:
public Image GetmageFromStream(string path)
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {
var img = Image.FromStream(fs);
return img;
}
}
I want to keep the last path which is selected. This is the code:
private void testFileButton_Click(object sender, EventArgs e)
{
fd = new OpenFileDialog();
fd.FileName = testParameters.testFileFile;
fd.InitialDirectory = testParameters.testFileDir;
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
if (fd.SafeFileName != null)
{
testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
testParameters.testFileFile = Path.GetFileName(fd.FileName);
testFileLabel.Text = fd.FileName;
}
}
catch (IOException)
{
MessageBox.Show("Error: Could not read file");
}
}
}
to be able to keep the last selected path, I tried to add RestorDirectory and index but I did not get any result:
private void testFileButton_Click(object sender, EventArgs e)
{
fd = new OpenFileDialog();
fd.FileName = testParameters.testFileFile;
fd.InitialDirectory = testParameters.testFileDir;
fd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
fd.FilterIndex = 2;
fd.RestoreDirectory = true;
if(...){
...
}
}
then I tried to use "else" instead:
private void testFileButton_Click(object sender, EventArgs e)
{
fd = new OpenFileDialog();
fd.FileName = testParameters.testFileFile;
fd.InitialDirectory = testParameters.testFileDir;
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
if (fd.SafeFileName != null)
{
testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
testParameters.testFileFile = Path.GetFileName(fd.FileName);
testFileLabel.Text = fd.FileName;
}
}
}
catch (IOException)
{
MessageBox.Show("Error: Could not read file");
}
}
else
{
openFileDialog1.InitialDirectory = #"C:\";
}
}
but again without any result..
Does anyone have any idea?
Editing: Last attemp
private void testFileButton_Click(object sender, EventArgs e)
{
//http://stackoverflow.com/questions/7793158/obtaining-only-the-filename-when-using-openfiledialog-property-filename
OpenFileDialog fd = new OpenFileDialog();
fd.FileName = testParameters.testFileFile;
Environment.CurrentDirectory = #"C:\" ;
if (fd.ShowDialog() == DialogResult.OK )
{
try
{
if (fd.SafeFileName != null)
{
string ffileName = fd.FileName;
testParameters.testFileDir = Path.GetDirectoryName(ffileName);
testParameters.testFileFile = Path.GetFileName(ffileName);
testFileLabel.Text = ffileName;
}
}
catch (IOException)
{
MessageBox.Show("Error: Could not read file");
}
}
}
The documentation states:
true if the dialog box restores the current directory to its original value if
the user changed the directory while searching for files; otherwise, false.
Update: Changing my answer a little, since I think I may have misunderstood your intention:
If I'm not mistaken, your dialog will open to fd.InitialDirectory each time you open it, since that is per definition the default for a new instance of fd. I believe this might be your problem here: You are calling fd = new OpenFileDialog(); each time you are trying to open it.
If you change your code to use the same instance for fd each time (define it in an outer scope, and e.g. access a singleton instance via a Property?), it might remember it's previous directory itself - that is the default behavior (which you can override using the RestoreDirectory property).
Example of getting a singleton instance: This will only instantiate the dialog once, and return the same instance each time you call the property:
Private OpenFileDialog _fd;
private OpenFileDialog SingleFd {
get { return _fd ?? (_fd = new OpenFileDialog()); }
}
// Now in your method, use:
var singleInstance = SingleFd;
You are not using same variable for file dialog everywhere. Like in if block you are showing file dialog fd, but in else part you are using variable openFileDialog. I couldn't understand why you are doing this, or probably it is a typo. Use same variable at both place if you want to set initial directory to "C:\" if user cancels the dialog.
Instead of creating instance in every call, create an instance once and use it for subsequent calls.
Regarding directory restore, if we don't set initial directory, by default it restores the previous directory, even for the different instances.
OpenFileDialog initially uses the current directory which is a process-wide setting. You have overridden this behavior by setting the InitialDirectory. Just don't do that and it will work.
If you want to persist the last directory used across process restarts, capture Environment.CurrentDirectory and save it. Set it before opening the dialog.
Note, that the current directory is a process-wide setting so that different parts of the app can interfere. Also, all relative paths are interpreted relative to this directory (which is why relative path are usually a bug waiting).
string path = #"C:\";
OpenFileDialog fd = new OpenFileDialog();
fd.FileName = "SelectFolder";
fd.InitialDirectory =path;
fd.ValidateNames = false;
fd.CheckFileExists = false;
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
if (fd.SafeFileName != null)
{
string txt1 = System.IO.Path.GetFullPath(fd.FileName),
txt2 = txt1.Replace("SelectFolder", "").Trim();
testFileLabel.Text = txt2.Replace(path, "").Replace(#"\", ""); ;
}
}
catch (IOException)
{
MessageBox.Show("Error: Could not read file");
}
}
The Solution is to set the InitialDirectory path Blank
openFileDialog.InitialDirectory = ""
openFileDialog.RestoreDirectory = True
I'm trying to use openFileDialog to open a Bitmap image and place it on my form. My form construtor...
public Form1()
{
InitializeComponent();
drawing = new Bitmap(drawingPanel.Width, drawingPanel.Height, drawingPanel.CreateGraphics());
Graphics.FromImage(drawing).Clear(Color.White);
// set default value for line thickness
tbThickness.Text = "5";
}
... opens a new form with a blank screen, and I can draw on it using the mouse and various color selector buttons. I then save the file with this method:
private void btnSave_Click(object sender, EventArgs e)
{
// save drawing
if (file == null) // file is a FileInfo object that I want to use
// to check to see if the file already exists
// I haven't worked that out yet
{
drawing.Save("test.bmp");
//SaveBitmap saveForm = new SaveBitmap();
//saveForm.Show();
}
else
{
drawing.Save(fi.FullName);
}
}
The image does save to the debug folder as a .bmp file. Then I use OpenFileDialog to open the file:
private void btnOpen_Click(object sender, EventArgs e)
{
FileStream myStream;
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "bmp files (*.bmp)|*.bmp";
if (openFile.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = (FileStream)openFile.OpenFile()) != null)
{
using (myStream)
{
PictureBox picBox = new PictureBox();
picBox.Location = drawingPanel.Location;
picBox.Size = drawingPanel.Size;
picBox.Image = new Bitmap(openFile.FileName);
this.Controls.Add(picBox);
}
}
}
catch (Exception ex)
{
}
}
}
What happes is that OpenFileDialog box comes up. When I select the file test.bmp, the screen goes away and then reappears, when I select it again, the OpenFileDialog window goes away and I'm back to my form with no image. Was hoping for some pointers. No compile or runtime errors.
Why are you calling ShowDialog() twice?
Just call ShowDialog once, so it doesn't open twice, like you indicated.
From MSDN:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
PictureBox picBox = new PictureBox();
picBox.Location = drawingPanel.Location;
picBox.Size = drawingPanel.Size;
picBox.Image = new Bitmap (myStream);
this.Controls.Add(picBox);
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
You open a dialog panel, then when it closes you check to see if the result was OK; then you open another new dialog in the using block; then you assign the image result of that to the PictureBox, and then throw everything away when the using block disposes.
You're calling ShowDialogue twice which is likely the source of your problem. Just use the following code, remove everything else from the method. Your use of using is also incorrect. it does clean up which is disposing of the results. You need to refactor or remove the using statement.
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog()
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox picBox = new PictureBox();
picBox.Location = drawingPanel.Location;
picBox.Size = drawingPanel.Size;
picBox.Image = new Bitmap (dlg.FileName);
this.Controls.Add(picBox);
}
}
}
The code above works but is without clean up or error handling. I'll leave that to you.