将RichTextBox FlowDocument保存到图像 [英] Saving RichTextBox FlowDocument to image

查看:119
本文介绍了将RichTextBox FlowDocument保存到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,我希望我的RichTextBox
内容(文本+图像)保存为图像(jpg / png)。我尝试使用此解决方案
但我只获得黑色填充图像

  SaveUIAsGraphicFile()

我还尝试从我的rtb控件创建FormattedText,打印工作正常,但不能在那里插入图像。也许有可能以某种方式打印FlowDocument?

解决方案

您可以使用类似下面的方法从FlowDocument创建位图:

  public BitmapSource FlowDocumentToBitmap(FlowDocument document,Size size)
{
document = CloneDocument(document);

var paginator =((IDocumentPaginatorSource)document).DocumentPaginator;
paginator.PageSize = size;

var visual = new DrawingVisual();
using(var drawingContext = visual.RenderOpen())
{
// draw white background
drawingContext.DrawRectangle(Brushes.White,null,new Rect(size));
}
visual.Children.Add(paginator.GetPage(0).Visual);

var bitmap = new RenderTargetBitmap((int)size.Width,(int)size.Height,
96,96,PixelFormats.Pbgra32);
bitmap.Render(visual);
返回位图;
}

public FlowDocument CloneDocument(FlowDocument document)
{
var copy = new FlowDocument();
var sourceRange = new TextRange(document.ContentStart,document.ContentEnd);
var targetRange = new TextRange(copy.ContentStart,copy.ContentEnd);

using(var stream = new MemoryStream())
{
sourceRange.Save(stream,DataFormats.XamlPackage);
targetRange.Load(stream,DataFormats.XamlPackage);
}

返回副本;
}

然后使用它如下所示将RichTextBox的文档保存到图像文件。

  var doc = richTextBox.Document; 
var bm = FlowDocumentToBitmap(doc,new Size(richTextBox.ActualWidth,richTextBox.ActualHeight));
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bm));

using(var stream = new FileStream(doc.jpg,FileMode.Create))
{
encoder.Save(stream);
}


i'am making a programm where i want my RichTextBox content (text+images) to be saved as an image (jpg/png). I tried to use this solution but i get only black filled image from

SaveUIAsGraphicFile() 

I also tried to create FormattedText from my rtb control, printing it works fine, but its not possible to insert images in there. Maybe it is possible to print FlowDocument somehow?

解决方案

You could use something like the following method to create a bitmap from a FlowDocument:

public BitmapSource FlowDocumentToBitmap(FlowDocument document, Size size)
{
    document = CloneDocument(document);

    var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
    paginator.PageSize = size;

    var visual = new DrawingVisual();
    using (var drawingContext = visual.RenderOpen())
    {
        // draw white background
        drawingContext.DrawRectangle(Brushes.White, null, new Rect(size));
    }
    visual.Children.Add(paginator.GetPage(0).Visual);

    var bitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height,
                                        96, 96, PixelFormats.Pbgra32);
    bitmap.Render(visual);
    return bitmap;
}

public FlowDocument CloneDocument(FlowDocument document)
{
    var copy = new FlowDocument();
    var sourceRange = new TextRange(document.ContentStart, document.ContentEnd);
    var targetRange = new TextRange(copy.ContentStart, copy.ContentEnd);

    using (var stream = new MemoryStream())
    {
        sourceRange.Save(stream, DataFormats.XamlPackage);
        targetRange.Load(stream, DataFormats.XamlPackage);
    }

    return copy;
}

and then use it like shown below to save a RichTextBox's Document to an image file.

var doc = richTextBox.Document;
var bm = FlowDocumentToBitmap(doc, new Size(richTextBox.ActualWidth, richTextBox.ActualHeight));
var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bm));

using (var stream = new FileStream("doc.jpg", FileMode.Create))
{
    encoder.Save(stream);
}   

这篇关于将RichTextBox FlowDocument保存到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆