Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a thumbnail of an inactive C# WPF Window

Tags:

c#

wpf

I've looked through many topics here, and googled for the information, but I haven't found anything relating to my question.

What I want to do is have it so when a user starts the application, the main window (not an MDI) opens with four imageboxes, each showing an image of the form that would open when they click on it. Once the selected form is open, and changes are made, if they click to minimize/close the form, it will (seemingly) minimize into the imagebox showing a real-time image of what the form looks like in a thumbnail view.

My question is, how do I make a form into an image so I can use the image as a thumbnail in an imagebox?

Also... Can someone point me in the direction of some resources that will help me figure out how to animate the "minimizing" into the imagebox?

I'm not asking anyone to do my work for me, because I'd like to learn it myself, but I'm kinda stuck.

Lastly, I'm not sure what's involved in this, so I don't know what tags to put for this post. I'll add tags as I figure it out so others can find this information.

EDIT: Sorry, it is in WPF. Wasn't sure it would be any different. I'm still not particularly experienced in WPF.

like image 630
Sivvy Avatar asked Jun 29 '10 14:06

Sivvy


1 Answers

You can use the VisualBrush, here is a quick example of a button with a background set to a downscaled version of a stackpanel.

 <DockPanel>
        <StackPanel x:Name="myRect" >
            <TextBox Text="MyTexasdfasdfasdfasdfasdft" Height="50" />
            <CheckBox IsChecked="True"  />
            <Rectangle Fill="Red" Width="100" Height="100" />
        </StackPanel>


        <Button>
            <Button.Background>
                <VisualBrush TileMode="None"  Viewport="0,0,1,1" Visual="{Binding ElementName=myRect}" >
                    <VisualBrush.Transform>
                        <ScaleTransform ScaleX="0.3" ScaleY="0.3" />
                    </VisualBrush.Transform>
                </VisualBrush>
            </Button.Background>
        </Button>
    </DockPanel>

Edit: though this solution works to copy stuff that is on the screen, when the stuff on screen is hidden or removed, so will the VisualBrush. In order to persist the image, it is necessary to render the control to a bitmap. This can be done with the RenderTargetBitMap

// CenterControl is the target to render, ShowControl is the control to render the CenterControl onto.
var rtb = new RenderTargetBitmap((int)CenterControl.ActualWidth, (int)CenterControl.ActualHeight, 96, 96,
                                             PixelFormats.Pbgra32);
            rtb.Render(CenterControl);
            var bgBrush = new ImageBrush(rtb) {Transform = new ScaleTransform(0.1, 0.1)};
            ShowControl.Background = bgBrush;
like image 126
hkon Avatar answered Sep 22 '22 17:09

hkon