1

The problem is that I am opening workflow designer dynamically from one shell application and I don't have reference to Canvas. I am able to save the WF4 as image but the image is not getting saved properly and contains left & top margins. I followed many articles to get it working but no success. I referred to following article as well.

Saving a canvas to png C# wpf

I am using the below function. I don't have any reference to canvas.

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

Please help on this.

Community
  • 1
  • 1
Parveen Kumar
  • 419
  • 3
  • 7
  • 20

2 Answers2

1

I am able to resolve the issue by referring to again the following link

Saving a canvas to png C# wpf

I got the reference to canvas by using following code

Visual canvas= ((DesignerView)VisualTreeHelper.GetChild(this.WorkflowDesigner1.View, 0)).RootDesigner;

This has resolved the border/margin issue.

Community
  • 1
  • 1
Parveen Kumar
  • 419
  • 3
  • 7
  • 20
0

Please have a look here: http://blogs.msdn.com/b/flow/archive/2011/08/16/how-to-save-wf4-workflow-definition-to-image-using-code.aspx

let’s look at how to generate an image of the workflow definition using the standard mechanism of WPF. After all, workflow designer canvas is a WPF control.

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

The above C# method is very straightforward. Just get the workflow diagram part of the workflow designer and create an in-memory image of it using some WPF API. The next thing is simple: create a file and save the image.

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();
    }
}

At last, let’s try to invoke the above 2 methods in the OnInitialized() method to hook it up and then close the application.

protected override void OnInitialized(EventArgs e)
{
    // ...
    this.SaveImageToFile("test.jpg", this.CreateWorkflowDefinitionImage());
    Application.Current.Shutdown();
}
billsecond
  • 612
  • 3
  • 21
  • 50
  • Thanks for the inputs. My requirement is that I am able to save the Workflow as image but the image contains left and top borders. Please let me know how to save the image with these borders. – Parveen Kumar Oct 14 '14 at 11:50