I update your Save method as below shown to implement want you want
public void Save(string fileName)
{
FrameworkElement element = this;
string imageExtension;
imageExtension = new FileInfo(fileName).Extension.ToLower(System.Globalization.CultureInfo.InvariantCulture);
BitmapEncoder imgEncoder;
switch (imageExtension)
{
case ".bmp":
imgEncoder = new BmpBitmapEncoder();
break;
case ".jpg":
case ".jpeg":
imgEncoder = new JpegBitmapEncoder();
break;
case ".png":
imgEncoder = new PngBitmapEncoder();
break;
case ".gif":
imgEncoder = new GifBitmapEncoder();
break;
case ".tif":
case ".tiff":
imgEncoder = new TiffBitmapEncoder();
break;
case ".wdp":
imgEncoder = new WmpBitmapEncoder();
break;
default:
imgEncoder = new BmpBitmapEncoder();
break;
}
if (element != null)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext context = drawingVisual.RenderOpen())
{
VisualBrush brush = new VisualBrush(element) { Stretch = Stretch.Fill };
context.DrawRectangle(brush, null, new Rect(0, 0, element.ActualWidth, element.ActualHeight));
context.Close();
}
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);
imgEncoder.Frames.Add(BitmapFrame.Create(bitmap));
using (Stream stream = File.Create(fileName))
{
imgEncoder.Save(stream);
}
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.