C# 将带有图像的RTF文件加载到控制台应用程序中的FlowDocument

C# 将带有图像的RTF文件加载到控制台应用程序中的FlowDocument,c#,.net,console,rtf,C#,.net,Console,Rtf,我正在创建一个简单的控制台应用程序,在这里我需要将RTF文件加载到FlowDocument以进行进一步的工作 我使用此代码将文件加载到FlowDocument: // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and defaul

我正在创建一个简单的控制台应用程序,在这里我需要将RTF文件加载到FlowDocument以进行进一步的工作

我使用此代码将文件加载到FlowDocument:

        // Create OpenFileDialog 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Set filter for file extension and default file extension 
        dlg.DefaultExt = ".rtf";
        dlg.Filter = "RichText Files (*.rtf)|*.rtf";

        // Display OpenFileDialog by calling ShowDialog method 
        Nullable<bool> result = dlg.ShowDialog();

        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            FlowDocument flowDocument = new FlowDocument();
            TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

            using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                textRange.Load(fileStream, DataFormats.Rtf);
            }

        }
//创建OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg=新的Microsoft.Win32.OpenFileDialog();
//为文件扩展名和默认文件扩展名设置筛选器
dlg.DefaultExt=“.rtf”;
dlg.Filter=“RichText文件(*.rtf)|*.rtf”;
//通过调用ShowDialog方法显示OpenFileDialog
可为空的结果=dlg.ShowDialog();
如果(结果==真)
{
//打开文件
字符串文件名=dlg.filename;
FlowDocument FlowDocument=新的FlowDocument();
TextRange TextRange=新的TextRange(flowDocument.ContentStart,flowDocument.ContentEnd);
使用(FileStream FileStream=File.Open(filename,FileMode.Open,FileAccess.Read,FileShare.Read))
{
Load(fileStream、DataFormats.Rtf);
}
}
如果我在WPF应用程序中执行此操作,并在flowDocumentPageViewer中显示文档,则一切正常。但若我尝试在控制台应用程序中加载文件,我会遇到异常:数据格式富文本框中的结构无法识别,参数名流

由于某些原因,只有当文档中有图像时,才会出现此异常


有什么问题吗?或者更好的解决方法是什么?

问题是在使用System.Windows namesapce作为数据格式时出现的。具有System.Windows.Forms功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Documents;

namespace RtfTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            DoRead();
        }

        private static void DoRead()
        {
            // Create OpenFileDialog 
            OpenFileDialog dlg = new OpenFileDialog();

            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".rtf";
            dlg.Filter = "RichText Files (*.rtf)|*.rtf";

            // Display OpenFileDialog by calling ShowDialog method 
            var result = dlg.ShowDialog();
            try
            {
                if (result == DialogResult.OK)
                {
                    // Open document 
                    string filename = dlg.FileName;
                    FlowDocument flowDocument = new FlowDocument();
                    TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);

                    using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        textRange.Load(fileStream, DataFormats.Rtf);
                    }

                }
            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}

System.Windows.Forms.DataFormats.Rtf
无法解决此问题。textRange不能包含图像。仍然得到
异常:数据格式富文本框中的结构无法识别,参数名称流