Completed
Last Updated: 27 Mar 2020 15:17 by ADMIN
Release R2 2020
ADMIN
Deyan
Created on: 18 Oct 2017 10:51
Category: WordsProcessing
Type: Bug Report
5
WordsProcessing: NotSupportedException is thrown when exporting document with WMF/EMF image to PDF
The current implementation uses System.Windows.Media.Imaging.BitmapImage class in order to take the image pixels for the PDF export. However, BitmapImage class throws NotSupportedException when being initialized with a WMF or EMF image.

WORKAROUND: WMF and EMF images may be converted to PNG images before the PDF export. The following code snippet shows how to convert all inline WMF image to PNG images by using System.Drawing.Image class:

private static void ConvertInlineWmfImagesToPng(RadFlowDocument document)
{
    foreach (ImageInline image in document.EnumerateChildrenOfType<ImageInline>())
    {
        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, ImageFormat.Png);
                    byte[] pngBytes = pngImageStream.ToArray();
 
                    image.Image.ImageSource = new ImageSource(pngBytes, "png");
                }
            }
        }
    }
}
1 comment
ADMIN
Lance | Manager - Technical Support
Posted on: 13 Mar 2019 21:47
This approach is a workaround for missing EMF images in the UI for ASP.NET RadEditor after importing a DOCX file.

Using the RadEditor's OnImportContent event, get the reference to the RadFlowDocument and iterate for ImageInline instances as Deyan shows.

protected void RadEditor1_ImportContent(object sender, EditorImportingArgs e)
{
    if (e.RadFlowDocument is RadFlowDocument importedDocument)
    {
        foreach (var inlineImage in importedDocument.EnumerateChildrenOfType<ImageInline>())
        {
            if (inlineImage.Image.ImageSource.Extension.Equals("emf", StringComparison.InvariantCultureIgnoreCase))
            {
                using (var emfImageStream = new MemoryStream(inlineImage.Image.ImageSource.Data))
                {
                    using (var pngImageStream = new MemoryStream())
                    {
                        var imageDrawing = System.Drawing.Image.FromStream(emfImageStream);
                        imageDrawing.Save(pngImageStream, ImageFormat.Png);
                        byte[] pngBytes = pngImageStream.ToArray();
 
                        inlineImage.Image.ImageSource = new ImageSource(pngBytes, "png");
                    }
                }
            }
        }
    }
}