How can I delete the original image and replace it with a new one but with the same name ?

Chocolade 536 Reputation points
2021-10-17T15:07:02.183+00:00
using System;  
using System.Collections.Generic;  
using System.Drawing;  
using System.Drawing.Text;  
using System.Globalization;  
using System.Text;  
using System.Windows;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
  
namespace Wpf_Download_Files  
{  
    class DrawOnImage  
    {  
        public void DrawText(String text, String path)  
        {  
            BitmapImage bi = new BitmapImage();  
            bi.BeginInit();  
            bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);  
            bi.EndInit();  
  
            string sText = text;  
            int nTextHeight = 60;  
            DrawingVisual drawingVisual = new DrawingVisual();  
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())  
            {  
                drawingContext.DrawImage(bi, new Rect(0, 0, bi.Width, bi.Height));  
  
                Rect rect = new Rect(0, bi.Height, bi.Width, nTextHeight);  
                drawingContext.DrawRectangle(System.Windows.Media.Brushes.LightGray, new System.Windows.Media.Pen(System.Windows.Media.Brushes.Red, 2), rect);  
  
                  
  
                FormattedText formattedText= new FormattedText(sText, CultureInfo.InvariantCulture,  
    FlowDirection.LeftToRight, new Typeface("Ariel"), 12, System.Windows.Media.Brushes.Red,  
    VisualTreeHelper.GetDpi(drawingVisual).PixelsPerDip);  
                  
                formattedText.TextAlignment = TextAlignment.Center;  
                double nX = bi.Width / 2;  
                double nY = bi.Height + nTextHeight / 2;  
                System.Windows.Point pt = new System.Windows.Point(nX, nY - formattedText.Height / 2);  
                drawingContext.DrawText(formattedText, pt);  
            }  
            RenderTargetBitmap bmp = new RenderTargetBitmap((int)bi.Width, (int)bi.Height + nTextHeight, 96, 96, PixelFormats.Pbgra32);  
            bmp.Render(drawingVisual);  
  
            GifBitmapEncoder gif = new GifBitmapEncoder();  
            gif.Frames.Add(BitmapFrame.Create(bmp));  
  
            System.IO.File.Delete(path);  
  
            using (System.IO.Stream stm = System.IO.File.Create(@"d:\Downloaded Images\Radar\radImage00001.gif"))  
            {  
                gif.Save(stm);  
            }  
        }  
    }  
}  

I'm getting exception that the file in path is busy when trying to delete it.

If not trying to delete it will work fine but will create new radImage00001.gif file and I want instead to replace the current image in path with the new one with the same name. not to create a new file but to replace.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Castorix31 91,536 Reputation points
    2021-10-17T16:39:14.277+00:00

    To unlock the file,

    Replace

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
    bi.EndInit();
    

    by

                BitmapImage bi = new BitmapImage();
                var stream = System.IO.File.OpenRead(path);
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.StreamSource = stream;
                bi.EndInit();
                stream.Close();
                stream.Dispose();
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. P a u l 10,766 Reputation points
    2021-10-17T15:53:20.363+00:00

    Is path being used in the calling code?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.