1

问题是我正在从一个 shell 应用程序动态打开工作流设计器,并且我没有对 Canvas 的引用。我可以将 WF4 保存为图像,但图像没有正确保存并且包含左和上边距。我关注了许多文章以使其正常工作,但没有成功。我也参考了下面的文章。

将画布保存为 png C# wpf

我正在使用以下功能。我对画布没有任何参考。

private BitmapFrame CreateWorkflowImage()
    {
    const double DPI = 96.0;
        Visual areaToSave = ((DesignerView)VisualTreeHelper.GetChild(this.wd.View,
        0)).RootDesigner;
        Rect bounds = VisualTreeHelper.GetDescendantBounds(areaToSave);
        RenderTargetBitmap bitmap = new RenderTargetBitmap((int)bounds.Width,
            (int)bounds.Height, DPI, DPI, PixelFormats.Default);
        bitmap.Render(areaToSave);
        return BitmapFrame.Create(bitmap);       
  }

请帮助解决这个问题。

4

2 回答 2

1

我可以通过再次参考以下链接来解决问题

将画布保存为 png C# wpf

我通过使用以下代码获得了对画布的引用

视觉画布= ((DesignerView)VisualTreeHelper.GetChild(this.WorkflowDesigner1.View, 0)).RootDesigner;

这解决了边界/边距问题。

于 2014-10-15T04:58:42.650 回答
0

请看这里:http: //blogs.msdn.com/b/flow/archive/2011/08/16/how-to-save-wf4-workflow-definition-to-image-using-code.aspx

让我们看看如何使用 WPF 的标准机制生成工作流定义的图像。毕竟,工作流设计器画布是一个 WPF 控件。

BitmapFrame CreateWorkflowDefinitionImage()
{
    const double DPI = 96.0;
    // this is the designer area we want to save
    Visual areaToSave = ((DesignerView)VisualTreeHelper.GetChild(
        this.workflowDesigner.View, 0)).RootDesigner;
    // get the size of the targeting area
    Rect size = VisualTreeHelper.GetDescendantBounds(areaToSave);
    RenderTargetBitmap bitmap = new RenderTargetBitmap((int)size.Width, (int)size.Height,
       DPI, DPI, PixelFormats.Pbgra32);
    bitmap.Render(areaToSave);
    return BitmapFrame.Create(bitmap);
}

上面的 C# 方法非常简单。只需获取工作流设计器的工作流图部分,并使用一些 WPF API 创建它的内存图像。接下来的事情很简单:创建一个文件并保存图像。

void SaveImageToFile(string fileName, BitmapFrame image)
{
    using (FileStream fs = new FileStream(fileName, FileMode.Create))
    {
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(fs);
        fs.Close();
    }
}

最后,让我们尝试在 OnInitialized() 方法中调用上述 2 个方法,将其挂起,然后关闭应用程序。

protected override void OnInitialized(EventArgs e)
{
    // ...
    this.SaveImageToFile("test.jpg", this.CreateWorkflowDefinitionImage());
    Application.Current.Shutdown();
}
于 2014-10-06T21:02:10.587 回答