New to Telerik Document ProcessingStart a free 30-day trial

Converting WMF Images to PNG in RTF Documents with RadWordsProcessing

Updated on Jun 5, 2026

Environment

VersionProductAuthor
2024.3.806RadWordsProcessingDesislava Yordanova

Description

This article demonstrates how to convert images in WMF format to PNG within an RTF document.

Solution

To convert WMF images to PNG format within an RTF document using RadWordsProcessing, follow these steps:

  1. Import the RTF file as a RadFlowDocument.

  2. Iterate through the floating images in the document.

  3. Convert each WMF image to PNG format.

  4. Export the modified document.

The following code snippet demonstrates this process:

csharp
using System.Diagnostics;
using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.Model.Shapes;
using Telerik.Windows.Documents.Media;

static void Main(string[] args)
{
    string inputFilePath = "yourfile.rtf";
    Telerik.Windows.Documents.Flow.Model.RadFlowDocument document;

    Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();

    using (Stream input = File.OpenRead(inputFilePath))
    {
        document = provider.Import(input);
    }

    ConvertInlineWmfImagesToPng(document);

    string outputFilePath = "converted.rtf";
    File.Delete(outputFilePath);
    using (Stream output = File.Create(outputFilePath))
    {
        provider.Export(document, output);
    }
    Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
}

private static void ConvertInlineWmfImagesToPng(RadFlowDocument document)
{
    foreach (FloatingImage image in document.EnumerateChildrenOfType<FloatingImage>())
    {
        if (image.Image.ImageSource.Extension.Equals("wmf", StringComparison.InvariantCultureIgnoreCase))
        {
            using (MemoryStream wmfImageStream = new MemoryStream(image.Image.ImageSource.Data))
            {
                using (MemoryStream pngImageStream = new MemoryStream())
                {
                    var imageDrawing = System.Drawing.Image.FromStream(wmfImageStream);
                    imageDrawing.Save(pngImageStream, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] pngBytes = pngImageStream.ToArray();

                    image.Image.ImageSource = new ImageSource(pngBytes, "png");
                }
            }
        }
    }
}

Notes

  • Add references to all required assemblies and namespaces, particularly those related to Telerik Document Processing and System.Drawing for image conversion.
  • The code snippet assumes the document contains a WMF image. Images in formats other than WMF are not affected.
  • This solution applies to floating images. For inline images, take a similar approach with slight adjustments to target ImageInline instances.

See Also