Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Extracting an image file from the content of a window?

 
 
Old 01-31-2006   #1 (permalink)
Jason Dolinger


 
 

Extracting an image file from the content of a window?

Hi all,

This is a question that I've asked before, but I never quite got an
answer. Does anyone know of a way given the WPF APIs where you can
extract and image representation of the visual display of a window?

Thanks,
Jason

My System SpecsSystem Spec
Old 01-31-2006   #2 (permalink)
Jason Dolinger


 
 

Re: Extracting an image file from the content of a window?

Keith Patrick wrote:
> You can paint with what's called a VisualBrush, which is "painting" using a
> set of Visuals (WPF components). The brush itself is your visual
> representation, although I'm not sure how sync'ed up it is with its parent
> Visual(s)
>
>


Hey Keith,

Thanks for the response. I've actually tried that, but it seems to
destroy that window that it sources its image content from (the window
turns all black!). That's why I'm looking to extract something which is
totally separate from the parent window, so won't have any effect on it.

Thanks,
Jason
My System SpecsSystem Spec
Old 01-31-2006   #3 (permalink)
Jason Dolinger


 
 

Re: Extracting an image file from the content of a window?

Keith Patrick wrote:
> You can paint with what's called a VisualBrush, which is "painting" using a
> set of Visuals (WPF components). The brush itself is your visual
> representation, although I'm not sure how sync'ed up it is with its parent
> Visual(s)
>
>


The actual code that does the above (originally provided by Drew Marsh)
is something like this :

Given that this code is executing inside the window that you want to
extract the image from.


VisualBrush vb = new VisualBrush();
vb.Visual = this; // sets the visual brush to the window


Rectangle rect = new Rectangle();
rect.Fill = vb;

The problem now is that I want my window's content to be repainted with
this VisualBrush that is filling the rectangle, as so:

this.Content = rect;

This actually results in a System.StackOverflowException though. I
believe the VisualBrush is continually monitoring it's visual at runtime
for any changes, which basically turns this approach into an infinite
loop. What I need is a way to cut the link between the visual brush and
the visual that it is attached (ie. when the Visual is set, take that
as the value and no longer check for any further changes).

Is this possible? I keep seeing references to something in WPF called a
Freezable, but I can't find a clear description of what these are. Is
this something that could be applied here?

Thanks for any help guys,
Jason
My System SpecsSystem Spec
Old 01-31-2006   #4 (permalink)
Drew Marsh


 
 

Re: Extracting an image file from the content of a window?

Jason Dolinger wrote:

> This actually results in a System.StackOverflowException though. I
> believe the VisualBrush is continually monitoring it's visual at
> runtime for any changes, which basically turns this approach into an
> infinite loop. What I need is a way to cut the link between the
> visual brush and the visual that it is attached (ie. when the Visual
> is set, take that as the value and no longer check for any further
> changes).


Yes VisualBrush is a "live" image of the Visual. In your scenario that means
you've got a child painting itself with a parent which obviously needs to
paint the child to be painted, so infinite recursion ensues and boom... stack
overflow.

> Is this possible? I keep seeing references to something in WPF called
> a Freezable, but I can't find a clear description of what these are.
> Is this something that could be applied here?


Yes it's possible. What you'll need to do is take a "snapshot" of the Window
at a certain point in time in form of a Bitmap. Search this newsgroup for
RenderTargetBitmap and you should find a couple lengthy discussions on how
to use that to get a Bitmap from any Visual. Once you have the Bitmap you
simply paint your rect with that. Make sure you read all of the threads on
RenderTargetBitmap because there are some important tricks to learn about
it before you get the result you're looking for.

HTH,
Drew

___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/


My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Various Errors - winlogon.exe Bad Image, Windows - Bad Image & Window Defender General Discussion
Files not extracting from zip file Vista file management
Set-Content not updating file after get-content and forEach-Object PowerShell
Extracting ISO image Vista installation & setup
Weirdness with get-content | replace | set-content - file content is deleted!! PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46