tags:

views:

36

answers:

1

I want to create the image of a dynamically created usercontrol and show it in a window.

I am creating the usercontrol using the Below code .

 MyViews.MyViewsUserControl myViewsCanvas = new MyViews.MyViewsUserControl(AllFoundationMyViewsViewModel,item.Id);
                        //myViewsCanvas.Height = 5;
                        //myViewsCanvas.Width = 5;
                        Size size = new Size(50, 50);
                        myViewsCanvas.Measure(size);
                        double width = myViewsCanvas.DesiredSize.Width;
                        double height = myViewsCanvas.DesiredSize.Height;
                        myViewsCanvas.Arrange(new Rect(new Point(), size));

Then i am creating the image of the myViewsCanvas and adding it to a view box of another usercontrol called _DashBoardUserControl using the below code.

 _DashBoardUserControl.Viewbox2.Child = CreateImage(myViewsCanvas);

Then i am adding the _DashBoardUserControl to a window.

UserControls.Controls.PopupWindow popup = new UserControls.Controls.PopupWindow();
        popup.PopupContent = _DashBoardUserControl;
        popup.ShowDialog();

The problem is, I can only see a portion of the Image. I guess that is because of the measure() and arrange() methods. Can anybody tell me about these methods or what size should i pass to these methods. Do i need to scale down the image? If yes how do i do that?

+1  A: 

The easiest way I know of is this:

Viewbox v = new Viewbox();
v.Child = uielem;
uielem.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
v.Measure(uielem.DesiredSize);
v.Arrange(new Rect(new Point(), uielem.DesiredSize));
v.UpdateLayout();
r.Render(v);

where uielem is the element you want to render and r is the RenderTargetBitmap. (v.UpdateLayout might not be needed there, but I'm not sure anymore).

Alex Paven
thanks man. it helped a lot.