---
title: "How can I print a canvas in wpf?"  
description: "How can I print a canvas in wpf?"  
author: "Takeshi Okada"  
published: 2013-08-16  
updated: 2013-08-17  
canonical: https://www.mindstick.com/forum/1383/how-can-i-print-a-canvas-in-wpf  
category: "wpf"  
tags: ["wpf"]  
reading_time: 1 minute  

---

# How can I print a canvas in wpf?

Hi [mindstick](https://yourviews.mindstick.com/view/84668/how-mindstick-is-used-for-marketing-purposes)!

I used these code in order to print out the UI. [Printing](https://www.mindstick.com/blog/63804/5-ways-you-can-use-printing-in-your-marketing-strategy) out is working, but if the size of paper is over, the UI cuts off in the middle of a [canvas](https://www.mindstick.com/interview/1550/what-the-use-of-canvas-element-in-html5).

Is there any possible way not to be cut off in the middle?

<--cs code-->

PrintDialog [dialog](https://www.mindstick.com/forum/562/jquery-dialog-box-not-closed-in-asp-dot-net-mvc-4) = new PrintDialog();

dialog.PrintVisual(lst , "print");

<--Xaml -->

<[ListView](https://www.mindstick.com/articles/12744/listview-in-android) Name="lst">

<Grid Name="grdPrint">

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition />

</Grid.RowDefinitions>

<Canvas Grid.Row="0" >

.......

</Canvas>

<HListBox x:Name="lstImage" ItemsSource="{[Binding](https://www.mindstick.com/blog/146/dynamic-binding-in-c-sharp) IMG, Mode=TwoWay}" Grid.Row="1" IsHitTestVisible="True">

<HListBox.ItemTemplate>

<DataTemplate>

<HImage Margin="0" Width="590" Height="590" Stretch="Fill" [Source](https://www.mindstick.com/interview/840/what-happens-if-the-both-source-and-destination-are-named-same)="{Binding IMG_PATH_NM, Converter={StaticResource StrUriConverter}}" Tag="{Binding IMG_PATH_NM}">

</HImage>

</DataTemplate>

</HListBox.ItemTemplate>

<HListBox.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel [Orientation](https://www.mindstick.com/interview/2604/what-is-orientation)="[Vertical](https://www.mindstick.com/forum/12951/how-to-fix-bootstrap-tab-vertical-with-text)" HorizontalAlignment="Center" IsHitTestVisible="True"/>

</ItemsPanelTemplate>

</HListBox.ItemsPanel>

</HListBox>

</Grid>

</ListView>

Thanks kin [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)

\

## Replies

### Reply by shreesh chandra shukla

Hi!

This method will print the canvas to PNG file.

```
public void ExportToPNG(string imgpath, Canvas surface){    Uri path = new Uri(imgpath);    if (path == null)        return;    Transform transform = surface.LayoutTransform;    surface.LayoutTransform = null;     Size size = new Size(surface.Width, surface.Height);    surface.Measure(size);    surface.Arrange(new Rect(size));    RenderTargetBitmap renderBitmap =        new RenderTargetBitmap(        (int)size.Width,        (int)size.Height,        96d,        96d,        PixelFormats.Pbgra32);    renderBitmap.Render(surface);    using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))    {        PngBitmapEncoder encoder = new PngBitmapEncoder();        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));        encoder.Save(outStream);    }    surface.LayoutTransform = transform;}
```

thanks


---

Original Source: https://www.mindstick.com/forum/1383/how-can-i-print-a-canvas-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
