-1

How to give print preview functionality for a complete window before it gets printed here is the code i have implemented for printing the complete window.

My Code Behind :

private void Canvas_Print_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PrintDialog printdlg = new PrintDialog();
            MainWindow win = new MainWindow();
            this.Width = 350;
            this.Background = Brushes.White;
            panel.ScrollToTop();
            panel.ScrollToLeftEnd();
            win.Tag = this;
            if (printdlg.ShowDialog() == true)
            {
                printdlg.PrintVisual(panel.Content as Visual, "MyApplication");
            }
        }
Krrish
  • 271
  • 3
  • 17
  • you can parhaps make use of visual brush. – pushpraj Jul 04 '14 at 06:47
  • But Visual Brush is for images like bitmap how it will be useful in this case – Krrish Jul 04 '14 at 07:11
  • There is a lot more to it than this. There is no built in functionality. One option is to use RenderTargetBitmap and create an screenshot (Image) of the current MainWindow and show it in a control. I would suggest Googling different options. – Kcvin Jul 04 '14 at 07:16

1 Answers1

0

Here is a sample for print preview kind of functionality

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <Viewbox>
        <Border BorderBrush="Gray"
                Background="White"
                BorderThickness="2"
                Margin=".2in">
            <Rectangle Width="8.5in"
                       Height="11in"
                       Margin=".2in">
                <Rectangle.Fill>
                    <VisualBrush Visual="{Binding ElementName=visualToPrint}"
                                 Stretch="Uniform" />
                </Rectangle.Fill>
            </Rectangle>
            <Border.Effect>
                <DropShadowEffect Opacity=".25" />
            </Border.Effect>
        </Border>
    </Viewbox>
    <TextBox Text="change this text"
             Grid.Row="1"
             x:Name="visualToPrint" />
</Grid>

I created a rectangle of the size of a paper (assuming 8.5" 11") and set the fill to VisualBrush which is rendering my visual visualToPrint (panel in your case).

Also wrapped it into a border for page like effect and whole into a ViewBox to enable resizing

result

result

i chose a textbox for example you can choose any visual of your choice in the visual brush eg Visual="{Binding ElementName=panel}"

pushpraj
  • 13,458
  • 3
  • 33
  • 50