XAML文件到图像,在后面绑定计算代码

时间:2018-09-17 12:26:19

标签: c# wpf image xaml data-binding

我有以下代码将XAML文件另存为Image-Stream,但未计算绑定。有什么建议吗?

// get byte-array from file
using (var xamlStream = new MemoryStream((byte[])value))
using (var stream = new MemoryStream())
{
    var thread = new Thread(() =>
    {
        // load xaml control from stream
        var control = (System.Windows.FrameworkElement)System.Windows.Markup.XamlReader.Load(xamlStream);

        if (control == null)
            return;

        control.DataContext =  new { Person = new { FIRSTNAME = "Test" } };
        control.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));

        // get size of control which would be needed by Window
        var controlSize = control.DesiredSize;
        var rect = new System.Windows.Rect(0, 0, controlSize.Width, controlSize.Height);

        // render XAML to bitmap
        var targetBitmap = new RenderTargetBitmap((int)controlSize.Width, (int)controlSize.Height, 120, 96, PixelFormats.Pbgra32);

        control.Arrange(rect);
        targetBitmap.Render(control);

        // convert to png and save to sream
        var png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(targetBitmap));
        png.Save(stream);
    });
    thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
    thread.Start();
    thread.Join();
}

XAML代码:

<Canvas  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Name="_PrintCanvas" Width="1585" Height="1000">
  <TextBlock FontSize="80" Foreground="Red"  Text="{Binding Person.FIRSTNAME}" Canvas.Top="800" Canvas.Left="70" Margin="0,0,0,0"/>
</Canvas>

有没有一种方法可以在后面的代码中显式计算绑定?

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案:UpdateLayout()之前缺少targetBitmap.Render(control);

因此,新代码如下:

            // get byte-array from file
            using (var xamlStream = new MemoryStream((byte[])value))
            using (var stream = new MemoryStream())
            {
                var dataContext = new DataPackageController();
                dataContext.SetData(context);

                var thread = new Thread(() =>
                {
                    // load xaml control from stream
                    var control = (System.Windows.Controls.Canvas)System.Windows.Markup.XamlReader.Load(xamlStream);
                    if (control == null)
                        return;

                    control.DataContext = new { Person = new { FIRSTNAME = "Test" } };
                    control.Measure(new System.Windows.Size(double.PositiveInfinity, double.PositiveInfinity));

                    // get size of control which would be needed by Window
                    var controlSize = control.DesiredSize;
                    var rect = new System.Windows.Rect(0, 0, controlSize.Width, controlSize.Height);

                    // render XAML to bitmap
                    var targetBitmap = new RenderTargetBitmap((int)controlSize.Width, (int)controlSize.Height, 120, 96, PixelFormats.Pbgra32);

                    control.Arrange(rect);
                    control.InvalidateVisual();
                    control.UpdateLayout();
                    targetBitmap.Render(control);

                    // convert to png and save to sream
                    var png = new PngBitmapEncoder();
                    png.Frames.Add(BitmapFrame.Create(targetBitmap));
                    png.Save(stream);
                });
                thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
                thread.Start();
                thread.Join();
            }