One of the benefits of using WF4 to create workflow is that it provides a graphical workflow designer. This means in most of the cases, developers don’t need to write code to define their workflows. Instead, they can use the graphical workflow designer we provided. This can not only dramatically improve the productivity but also bring down the barrier of authoring workflows so that even non-developers can do so. Another advantage of this kind of graphical design is that it’s intuitive and easy to understand. This is why it’s been used so far in almost all kinds of workflow solutions.
In some scenarios, you may want to save your workflow definitions to images so that you can use them later. For example:
You may have already noticed that you can save the workflow definition to an image by right click on the designer surface and like Copy as Image or Save as Image. However, this is a manual approach. What if I want to write some code to save the images? You may ask.
Good news is, you can do it. Bad news is, you need to write some code, maybe a little bit more than you expected.
“What the hack?” You may wonder. Shouldn’t there be a public API for me to use? Even WF 3.x has that API. Well, it appears to be it should till you get to know why we didn’t provide one. But hanging there. It’s not hard to implement it by yourself. I promise. It will also help you to have a better understanding about how workflow designer works.
WF4 workflow designer is written in WPF while WF 3.x workflow designer is written in WinForm. This is a very significant difference. WPF doesn’t support generating an image of the UI when the UI itself has been drawn yet. So in order to save a workflow definition to an image, you need to create and actually make WPF draw the workflow definition in a workflow designer first.
This is the main reason why we didn’t provide any public API for saving as image. Because even if we did, you will only be able to use it after the workflow designer is opened, which is confusing. And it’s hard to imagine Microsoft will release an API in .NET Framework saying “You have to call it after you open the workflow designer UI”. That’s just bad API.
OK, knowing why we didn’t provide a public API for this. Let’s look at how we are going to achieve the goal by ourselves. At the high level,