Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否使用viewbox将XAML保存到png?_C#_Xaml - Fatal编程技术网

C# 是否使用viewbox将XAML保存到png?

C# 是否使用viewbox将XAML保存到png?,c#,xaml,C#,Xaml,我有一个带有多行文本框的简单表单 那么我有这个代码: using (Stream stream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) { writer.WriteLine(textBox1.Text); writer.Flush(); stream.Position = 0;

我有一个带有多行文本框的简单表单

那么我有这个代码:

using (Stream stream = new MemoryStream())
{
    using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
    {
        writer.WriteLine(textBox1.Text);
        writer.Flush();
        stream.Position = 0;
        FrameworkElement element = XamlReader.Load(stream) as FrameworkElement;


        Size renderingSize = new Size(128, 128);
        element.Measure(renderingSize);
        Rect renderingRectangle = new Rect(renderingSize);
        element.Arrange(renderingRectangle);
        element.UpdateLayout();

        Rect bounds = VisualTreeHelper.GetDescendantBounds(element);
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)element.ActualWidth,
            (int)element.ActualHeight,
            96, 96, PixelFormats.Pbgra32);

        DrawingVisual visual = new DrawingVisual();
        using (DrawingContext context = visual.RenderOpen())
        {
            VisualBrush brush = new VisualBrush(element);
            context.DrawRectangle(brush, null, new Rect(new Point(), bounds.Size));
        }
        renderBitmap.Render(visual);
        try
        {
            PngBitmapEncoder enc = new PngBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create(renderBitmap));
            using (Stream outputStream = File.OpenWrite(@"T:\test.png"))
            {
                enc.Save(outputStream);
            }
        }
        catch (ObjectDisposedException)
        {
            // IF the operation lasted too long, the object might be disposed already
        }
    }
}
我的文本框1的内容如下

<?xml version="1.0" encoding="utf-8"?>
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Canvas Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0">
    <Path Width="24" Height="24" Canvas.Left="12" Canvas.Top="12" Stretch="Fill" Fill="#FF000000" Data="F1 M 22,12L 26,12L 26,22L 36,22L 36,26L 26,26L 26,36L 22,36L 22,26L 12,26L 12,22L 22,22L 22,12 Z "/>
  </Canvas>
</Viewbox>

如果我运行这段代码,viewbox就不会产生任何效果,但是如果我使用这段xaml代码并将代码粘贴到expression blend中(删除xml头),那么我就会得到我想要的结果(内容填满整个屏幕)

那么为什么viewbox在我的代码中不起作用我缺少了什么