0

I'm trying to compare two images and delete the second one if they are the same image. When my program goes to delete a file, it throws an error: The process cannot access the file "C:\Temp\Image.jpg" because it is being used by another process

It seems to be an issue with this method not closing the bitmap file, but I have yet to find out a way to release the bitmap from system memory in order to delete it

    public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
    {
        MemoryStream ms = new MemoryStream();
        firstImage.Save(ms, ImageFormat.Png);
        string firstBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;

        secondImage.Save(ms, ImageFormat.Png);
        string secondBitmap = Convert.ToBase64String(ms.ToArray());

        if (firstBitmap.Equals(secondBitmap))
        {
            ms.Close();
            return true;
        }
        else 
        {
            ms.Close();
            return false;
        }
    }
Zach R
  • 181
  • 3
  • 15
  • 2
    Possible duplicate of [Free file locked by new Bitmap(filePath)](https://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath) – user247702 Nov 14 '18 at 14:40
  • 2
    You should also familiarise yourself with `IDisposable`, take a look at [What are the uses of “using” in C#](https://stackoverflow.com/questions/75401/what-are-the-uses-of-using-in-c-sharp) for an introduction. – user247702 Nov 14 '18 at 14:44
  • 1
    The code you should change is out of this scope. Show us how this is called (with the code that loads the bitmaps) – Jeroen van Langen Nov 14 '18 at 14:47
  • J. van Langen is correct; the shown code isn't the one where the problem occurs. That would be the code opening and deleting the images. As side note, why would you compare them as strings? Just use the byte arrays themselves and use `.SequenceEquals()`. Using string operations for stuff like that is usually a dirty programmer shortcut that is quite expensive on a processing level. – Nyerguds Nov 26 '18 at 15:22

2 Answers2

0

Dispose the bitmap object of second image before deleting the actual file. So something like 'secondImage.Dispose()'

pull420
  • 88
  • 2
  • 8
  • Or, the more sane way: use the images in `using` blocks so the objects get disposed automatically at the exact moment their scope ends. It has the added advantage of automatic cleanup in case any kind of exception occurs. – Nyerguds Nov 26 '18 at 15:24
0

I would reccommend to use try cach, and run dispose, to free the resources in finnaly section. This will dispose the object even if exception was thrown:

    public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
    try
    {
        MemoryStream ms = new MemoryStream();
        firstImage.Save(ms, ImageFormat.Png);
        string firstBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;

        secondImage.Save(ms, ImageFormat.Png);
        string secondBitmap = Convert.ToBase64String(ms.ToArray());

        if (firstBitmap.Equals(secondBitmap))
        {
            return true;
        }
        else 
        {           
            return false;
        }
    } 
    catch(Exception ex)
    {
        //log it, display or whatever
    } 
    finnaly 
    {
        ms.Close();
        ms.Dispose();
    }

}
artsch
  • 225
  • 2
  • 10