wpf - 将 WPF 路径转换为位图文件

标签 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);
        }
    } 
}

最佳答案

经过多次谷歌搜索和反复试验,我似乎已经找到了解决方案。此 post为我指明了正确的方向。
有几个问题:

  • 我现在正在设置路径和容器的大小
  • 堆栈面板容器有一些导致问题的细微差别,因此我将其替换为 Canvas
  • 最重要的是,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();
        }
    

    关于wpf - 将 WPF 路径转换为位图文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9607627/

    相关文章:

    c# - 在后台线程 WP7 上创建 BitmapImage

    java - 准备位图 byte[] 数据以传递给 jni

    .net - 单击后如何保持打开 rad 菜单?

    wpf - 设置内容控制的背景

    java - android项目的基本文件夹的路径是什么?

    java - Android - 获取从文件资源管理器中选择的 .txt 文件的真实路径

    c# - WPF:DoubleAnimation "To"属性绑定(bind)计时

    c# - 如何在 WPF 中刷新 DataGrid

    python - 查找Python可执行文件的路径

    c - 如何将 4 位字符数组转换为 Int?