I am using Visual C#

When i open file from my database then an image is loaded in my program,and image source is given below

map.Source = new BitmapImage(new Uri(filePath)); /// filepath is address of image from datebase

and when replace it by another image. The new image is loaded in my picture box,

then map.Source = new BitmapImage(new Uri(filePath)); //filepath is address of new image from disk

and when i try to delete previous image from my database then it show a message that the previous is being used.

How can I make image source of previous image free??

I think solution Taken from here should work.

private void LoadMapImage(string filepath)
        {
            if (!string.IsNullOrEmpty(filepath))
            {
                FileInfo info = new FileInfo(filepath);
                if (info.Exists && info.Length > 0)
                {
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.DecodePixelWidth = (int)Map.Width;
                    bi.CacheOption = BitmapCacheOption.OnLoad;
                    bi.UriSource = new Uri(info.FullName);
                    bi.EndInit();
                    Map.Source = bi;
                }
            }
        }

use Dispose()

for example:

Bitmap bmp = new Bitmap(FileUpload1.FileContent);
        bmp.Dispose();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.