1

問題は、ワークフロー デザイナーを 1 つのシェル アプリケーションから動的に開いていて、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

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 に答える