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.