Reader Level:
ARTICLE

Printing in WPF

Posted by Ghanashyam Nayak Articles | Printing in C# February 14, 2012
In this article, you will learn how to print the controls of a form & also the whole form all controls.
  • 1
  • 0
  • 14046
Download Files:
 

Let's first look at the form:

01.png

Now the code.

In my previous articles, I have written about how to print a form all controls & one control in the Simple Window C# Form.

Now in this article we will see about the printing of the controls & form in WPF in C#.

First of all add the items in the listbox.

myListBox.Items.Add("Item1");

myListBox.Items.Add("Item2");

myListBox.Items.Add("Item3");

myListBox.Items.Add("Item4");

myListBox.Items.Add("Item5");

myListBox.Items.Add("Item6");

Now load the image into the image control:

First of all create the object of the BitmapImage.

BitmapImage myBitmapImage = new BitmapImage();

Then assign the image path to the object of the Bitmap Image.

myBitmapImage.BeginInit();

myBitmapImage.UriSource = new Uri(@"C:\Users\Indus_User\Documents\Visual Studio 2010\Projects\WpfPrint\WpfPrint\Images\Jellyfish.jpg");

myBitmapImage.EndInit();

Then assign that object of the BitmapImage to the Image Control.

myImage.Source = myBitmapImage;

Now for printing.

In WPF it's easy to print the Simple Window form in C#.

First of all print for the ListBox.

PrintDialog myPrintDialog = new PrintDialog();

if (myPrintDialog.ShowDialog() == true)

{

myPrintDialog.PrintVisual(myListBox, "Listbox Items Print");

}

Take the object of the PrintDialog then using the method "PrintVisual" for printing the Listbox.

In the PrintVisual method we have to pass the two parameters. The first one is the Listbox name & the second one is the description of the job that is printing.

Now print the Image.

PrintDialog myPrintDialog = new PrintDialog();

if (myPrintDialog.ShowDialog() == true)

{

myPrintDialog.PrintVisual(myImage, "Image Print");

}

In the above code we have to just change the name instead of the listbox, we use the image control name.

In the same way we can print the whole form including all controls. Instead of the control object we write the "this".

PrintDialog myPrintDialog = new PrintDialog();

if (myPrintDialog.ShowDialog()==true)

{

myPrintDialog.PrintVisual(this, "Form All Controls Print");

}

The XAML code :

<Window x:Class="WpfPrint.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Print Controls" Height="350" Width="525" Loaded="Window_Loaded">

    <Grid>

        <ListBox Height="118" HorizontalAlignment="Left" Margin="20,24,0,0" Name="myListBox" VerticalAlignment="Top" Width="120" />

        <Image Height="190" HorizontalAlignment="Left" Margin="178,24,0,0" Name="myImage" Stretch="Fill" VerticalAlignment="Top" Width="255" />

        <Button Content="Print Image" Height="34" HorizontalAlignment="Left" Margin="178,220,0,0" Name="myButtonPrintImage" VerticalAlignment="Top" Width="255" Click="myButtonPrintImage_Click" />

        <Button Content="Print Listbox" Height="34" HorizontalAlignment="Left" Margin="20,148,0,0" Name="myButtonPrintListBox" VerticalAlignment="Top" Width="120" Click="myButtonPrintListBox_Click" />

        <Button Content="Print Form" Height="34" HorizontalAlignment="Left" Margin="127,265,0,0" Name="myButtonPrintForm" VerticalAlignment="Top" Width="255" Click="myButtonPrintForm_Click" />

    </Grid>

</Window>

See the following images :

Printing of the Listbox.

02.png

03.png

04.png

Printing of the image.

05.png

06.png

Printing of all the controls of the whole form.

07.png

08.png


Hope this will helpful to you guys...

COMMENT USING

Trending up