.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Rendering controls (Custom Panel) into image
 

Rendering controls (Custom Panel) into image

Hi all,

Here is the situation: I have a custom panel which has 2 different layout modes. The mode is switched by bool variable. The custom panel is a logical child of lets say some stackpanel:

Code Snippet

<StackPanel>

<CustomPanel Mode="True">

<Image>

<!-- The Image must contain CustomPanel "photo"rendered in another mode-->

</StackPanel>

Now what I need to do - to have somewhere other element - possibly Image, where I can render another mode of one single instance of my custom Panel. I need image only - it should not be a functional control.

I know I could use VisualBrush for that purpose but it always renders the same mode, at least I had no luck to make it rendering different modes.

Also I tried VisualTreeHelper.GetDrawing(Visual visual) but it's not that easy, becausethe result GrawingGroupdoes not contain VisualChildren's Drawings. Though I could make a recursive function which will get full DrawingGroup of my control if there is no other way to do what I need.

Any ideas?

Thanks

Anton

Anton Khodakivskiy

Yes it's possible, but I think you're probably better off having two instances. Here's the only approach I can think to try:

  1. Remove the control from the logical tree
  2. Change the layout type
  3. Call UpdateLayout
  4. Setup a RenderTargetBitmap and .Render(yourControl)
  5. Change the layout type back
  6. Put your control back into the logical tree
  7. Take the RTB from 4 and hook it to an Image and throw that in the logical tree wherever you want it displayed

HTH,
Drew

Drew Marsh
I don't think there's any way to do what you want. The reason is because layout != rendering. An instance of a control in the tree can only lay itself out one way at a time. It can be rendered many times over using VisualBrush, but it can only be laid out once.


HTH,
Drew
Drew Marsh
Do you think it's possible to lay outcontrol without rendering? If this is possible then I'd relayout layout my control,update visual brush on the Image and lay it out back
Anton Khodakivskiy

Yes it's possible, but I think you're probably better off having two instances. Here's the only approach I can think to try:

  1. Remove the control from the logical tree
  2. Change the layout type
  3. Call UpdateLayout
  4. Setup a RenderTargetBitmap and .Render(yourControl)
  5. Change the layout type back
  6. Put your control back into the logical tree
  7. Take the RTB from 4 and hook it to an Image and throw that in the logical tree wherever you want it displayed

HTH,
Drew

Drew Marsh

Thanks Drew, that's probably what I was looking for. Hopeit wont be blinking because of removing the contol from the logical tree.

Edit..

No way.. it's too slow. it takes several seconds to rendermy custom panel.. =/

Hm weird.. Im wondering how does VisualBrush work....

Another edit: I was wrong about several seconds. I was calling RenderTargetBitmap.Render() each time my panel's layout was updated (handled according event) which occurs about 100 times/second.

looks like the whole method is absolutely acceptable.

Thanks again, Drew.

Anton Khodakivskiy

You can use google to search for other answers

Custom Search

More Threads

• Need Help Please - Control Visibility
• Raising an event when a Storyboard hits a certain time value
• Cyclic reference found while evaluating the Theme Style property on element
• Visual Studio 2005 : WPF Extenion
• Frame Disappearing on Clicking the ListViewItem
• Using Thumb to drag elements, how?
• XAML inheriting from another XAML file?
• WPF Label with a running content.
• How to make a drag element?
• From object in header to expander