将WPF路径转换为位图文件

时间:2012-03-07 19:15:39

标签: wpf path bitmap render resourcedictionary

我希望能够加载路径的WPF资源字典并将它们逐个输出到文件(jpg,bmp,它并不重要)。这将在一个类库中,MVC应用程序将访问该类以呈现为http流,因此我完全在代码中执行此操作(无XAML页面)。

我已经能够加载字典并遍历路径,但是当我将图像保存到磁盘时,它们是空白的。我知道我遗漏了一些微不足道的东西,比如将路径应用到一个几何体上,或者将它添加到一些包含矩形的东西中,但我的WPF体验有些限制。

我使用以下代码:

我有一个包含多个路径的WPF资源字典,例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Path x:Key="Path1" Data="M 100,200 C 100,25 400,350 400,175 H 280" Fill="White" Margin="10,10,10,10" Stretch="Fill"/>
  <Path x:Key="Path2" Data="M 10,50 L 200,70" Fill="White" Margin="10,10,10,10" Stretch="Fill"/>
</ResourceDictionary>

读取和输出文件的类:

public class XamlRenderer
{
    public void RenderToDisk()
    {
        ResourceDictionary resource = null;

        Thread t = new Thread(delegate()
        {
            var s = new FileStream(@"C:\Temp\myfile.xaml", FileMode.Open);
            resource = (ResourceDictionary)XamlReader.Load(s);
            s.Close();

            foreach (var item in resource)
            {
                var resourceItem = (DictionaryEntry)item;
                var path = (System.Windows.Shapes.Path)resourceItem.Value;

                var panel = new StackPanel();

                var greenBrush = new SolidColorBrush {Color = Colors.Green};

                path.Stroke = Brushes.Blue;
                path.StrokeThickness = 2;
                path.Fill = greenBrush;

                panel.Children.Add(path);

                panel.UpdateLayout();

                string filepath = @"C:\Temp\Images\" + resourceItem.Key + ".jpg";

                SaveImage(panel, 64, 64, filepath);
            }
        });

        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    public void SaveImage(Visual visual, int width, int height, string filePath)
    {
        var bitmap =
            new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
        bitmap.Render(visual);

        var image = new PngBitmapEncoder();
        image.Frames.Add(BitmapFrame.Create(bitmap));
        using (Stream fs = File.Create(filePath))
        {
            image.Save(fs);
        }
    } 
}

1 个答案:

答案 0 :(得分:6)

经过大量的谷歌搜索和反复试验后,我似乎已经找到了解决方案。这post指出了我正确的方向。 有一些问题:

  • 我现在正在设置路径和容器的大小
  • 堆栈面板容器有一些细微差别导致问题,所以我用画布替换它
  • 最重要的是,需要在容器元素上调用Measure()Arrange()。不需要UpdateLayout()来电。

修复这些问题后,将图像渲染到磁盘上(尽管我还有一个宽高比问题尚未解决)。

以下是更新后的代码:

    public void RenderToDisk()
    {
        ResourceDictionary resource = null;

        Thread t = new Thread(delegate()
        {
            var s = new FileStream(@"C:\Temp\myfile.xaml", FileMode.Open);
            resource = (ResourceDictionary)XamlReader.Load(s);
            s.Close();

            foreach (var item in resource)
            {
                var resourceItem = (DictionaryEntry)item;
                var path = (System.Windows.Shapes.Path)resourceItem.Value;

                path.Margin = new Thickness(10);
                path.HorizontalAlignment = HorizontalAlignment.Center;
                path.VerticalAlignment = VerticalAlignment.Center;
                path.Width = 48;
                path.Height = 48;
                path.Stroke = Brushes.White;
                path.Fill = Brushes.Black;

                var canvas = new Canvas();
                canvas.Width = 64;
                canvas.Height = 64;
                canvas.Margin = new Thickness(0);
                canvas.Background = Brushes.Transparent;

                canvas.Children.Add(path);

                canvas.Measure(new Size(canvas.Width, canvas.Height));
                canvas.Arrange(new Rect(new Size(canvas.Width, canvas.Height)));

                string filepath = @"C:\Temp\Images\" + resourceItem.Key + ".png";

                SaveImage(canvas, (int)canvas.Width, (int)canvas.Height, filepath);
            }
        });

        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }