question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked Paul-5034 edited

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

 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.

dotnet-csharpwindows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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();
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 edited

Is path being used in the calling code?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yes I'm calling the method from the MainWindow.xaml like this :

 InitializeComponent();
    
             DrawOnImage drawonimage = new DrawOnImage();
             drawonimage.DrawText("Hello World", @"d:\Images\radImage0.gif");

I want to change the radImage0.gif and keep it's name not to create a new gif file.

0 Votes 0 ·

It looks like you need to set the CacheOption of the BitmapImage to be OnLoad, i.e:

bi.CacheOption = BitmapCacheOption.OnLoad;


Otherwise the file stream will be left open after the initialisation has ended (until the garbage collector removes it):
https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.bitmapimage.cacheoption?view=windowsdesktop-5.0#remarks

0 Votes 0 ·