Export view (not rendered) to image

Topics: Framework Services, Getting Started, UI Architecture
Aug 19, 2011 at 3:19 PM


  I am trying to get a view exported (for example to a jpg file) out of a program that uses Caliburn Micro.
  It is getting complicated because the view is not yet rendered! So I want to create the view in code, pass the data in and then export this view.
  My best result I had was using:

MyView view = new MyView();
MyViewModel viewModel = new MyViewModel(inputData);
ViewModelBinder.Bind(viewModel, view, null);
view.Measure(new Size(width, height));
view.Arrange(new Rect(new Point(), new Size(width, height));

I get an image but unfortunately only with the static content.

All the parts that need databinding are missing!
Can anyone help here? Has anybody tried something like this? Do I have to use the ActivateItem method? Any ideas, help?

Thanks really a lot in advance,
Aug 19, 2011 at 3:31 PM

Does your Export method wrap the view inside a VisualBrush? I fear that parts of the UI are not rendered unless a proper visual tree is detected.

I was able to achieve something like you need, I'm digging my code to try to find a simple example.

Aug 19, 2011 at 4:35 PM

Wow, that was a really fast reply! :) Thanks a lot!
The Export method is not much more than:

WriteableBitmap bitmap = new WriteableBitmap(width, height);
bitmap.Render(surface, transTrans);
SaveToFile(bitmap, outStream);

So to answer your question: I am not working with a VisualBrush.
Thanks for digging! That would be really awesome if you would find something!!!
(because concerning this topic I only make negative progress.)
Does one have to wait for the databinding to finish?

A big thanks again BladeWise!

Aug 19, 2011 at 6:12 PM

Well, while digging through my code I realized that my scenario was quite different from yours: I needed to print a visual object that could be currently unloaded. It seems that in my case the control is loaded at least one time before being rendered. In other words, my scenario is different from yours, since my control is loaded at least once.

This leads to a new question: have you checked if the view is loaded at least once? I suppose that the control is never loaded (this is an assumption, I have not verified it), preventing some logic in both the .NET framework and CM to work (I must say that the only logic that depends on the Loaded event is used either in the Action or in the Bind.Model, so unless you are using a view-first approach in some of your views, CM should NOT be the cause of the issue).

Could you try to force a load of the view (adding it to a temporary Window, for example), and check if the exported image is as expected?

Aug 19, 2011 at 7:36 PM

I don't know if I understood you correctly but in fact I want to generate this view without the program itself ever being loaded, for example to generate server-generated content.
As I wrote in my initial post, I get an image with the control but all the databinding stuff is missing. For that I thought, that the databinding is perhaps happening too late?!?!

A general question: Is my scenario so uncommon? I have a fancy looking user defined - for example - dashboard with a nice header, some charts and a few tables.
This dashboard definition is stored on the server. If the user now wants to get an image of this dashboard with current data every day per Mail it has to be server generated and then mailed. No UI at all! How is something like this possible?

But I see that this question is not really caliburn relevant - although; if someone has an idea I will be thankful! :)
BladeWise - a big thanks to you again!