COUT COUT - 3 months ago 19
C# Question

Memory/Resources not being released

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);
}
}
}
}

Answer

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.

Comments