Snapshots of Control in time using VisualBrush sto

2020-03-24 04:16发布

I need to take snapshots of Control in time and store them in one FixedDocument. Problem is that VisualBrush is somehow "lazy" and do not evaluate itself by adding it to document. When I finaly create the document, all pages contains the same (last) state of Control. While VisualBrush cannot be Freezed, is there any other chance to do it? I would like to have more snapshots on one page so generate document page by page isn't solution for me. Aswel as converting VisualBrush to Bitmap (I want to keep it in vectors). In short - I need to somehow Freeze() VisualBrush

for(;;)
{
    FixedPage page = new FixedPage();
    ...
    Rectangle rec = new Rectangle();
    ...
    rec.Fill = vb;
    page.Children.Add(rec);
    PageContent content = new PageContent();
    ((IAddChild)content).AddChild(page);
    doc.Pages.Add(content);
}

1条回答
Juvenile、少年°
2楼-- · 2020-03-24 04:42

I used serialization:

string svb = XamlWriter.Save(vb.CloneCurrentValue());
// Replace all "Name" attributes (I don't need them already and deserialization would crash on them) with "Tag" - not best practice but it's fast :)
svb = svb.Replace("Name", "Tag");
rect.Fill((VisualBrush)XamlReader.Parse(svb));

EDIT

Better way is to save Visual as XPS document and then take the Visual back. (De)serialization has some problems with SharedSizeGroups and many other "reference like" things.

XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
control.InvalidateArrange();
UpdateLayout();
writer.Write(control);
Visual capture = doc.GetFixedDocumentSequence().DocumentPaginator.GetPage(0).Visual;
查看更多
登录 后发表回答