Printing in WPF

Let's first look at the form:
 
01.png
 
Now the code.
 
In my previous article, 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.
  1. myListBox.Items.Add("Item1");  
  2. myListBox.Items.Add("Item2");  
  3. myListBox.Items.Add("Item3");  
  4. myListBox.Items.Add("Item4");  
  5. myListBox.Items.Add("Item5");  
  6. myListBox.Items.Add("Item6");  
Now load the image into the image control:
 
First of all create the object of the BitmapImage.
  1. BitmapImage myBitmapImage = new BitmapImage();  
Then assign the image path to the object of the Bitmap Image.
  1. myBitmapImage.BeginInit();  
  2. myBitmapImage.UriSource = new Uri(@"C:\Users\Indus_User\Documents\Visual Studio 2010\Projects\WpfPrint\WpfPrint\Images\Jellyfish.jpg");  
  3. myBitmapImage.EndInit();  
Then assign that object of the BitmapImage to the Image Control.
  1. 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.
  1. PrintDialog myPrintDialog = new PrintDialog();  
  2. if (myPrintDialog.ShowDialog() == true)  
  3. {  
  4.     myPrintDialog.PrintVisual(myListBox, "Listbox Items Print");  
  5. }  
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.
  1. PrintDialog myPrintDialog = new PrintDialog();  
  2. if (myPrintDialog.ShowDialog() == true)  
  3. {  
  4.     myPrintDialog.PrintVisual(myImage, "Image Print");  
  5. }   
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".
  1. PrintDialog myPrintDialog = new PrintDialog();  
  2. if (myPrintDialog.ShowDialog()==true)  
  3. {  
  4.     myPrintDialog.PrintVisual(this"Form All Controls Print");  
  5. }  
The XAML code :
  1. <Window x:Class="WpfPrint.MainWindow"      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  2.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  3.         Title="Print Controls" Height="350" Width="525" Loaded="Window_Loaded">  
  4.     <Grid>  
  5.         <ListBox Height="118" HorizontalAlignment="Left" Margin="20,24,0,0" Name="myListBox" VerticalAlignment="Top" Width="120" />  
  6.         <Image Height="190" HorizontalAlignment="Left" Margin="178,24,0,0" Name="myImage" Stretch="Fill" VerticalAlignment="Top" Width="255" />  
  7.         <Button Content="Print Image" Height="34" HorizontalAlignment="Left" Margin="178,220,0,0" Name="myButtonPrintImage" VerticalAlignment="Top" Width="255" Click="myButtonPrintImage_Click" />  
  8.         <Button Content="Print Listbox" Height="34" HorizontalAlignment="Left" Margin="20,148,0,0" Name="myButtonPrintListBox" VerticalAlignment="Top" Width="120" Click="myButtonPrintListBox_Click" />  
  9.         <Button Content="Print Form" Height="34" HorizontalAlignment="Left" Margin="127,265,0,0" Name="myButtonPrintForm" VerticalAlignment="Top" Width="255" Click="myButtonPrintForm_Click" />  
  10.     </Grid>  
  11. </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 be helpful to you guys...


Similar Articles