Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from file and print it using WPF... how?

Tags:

printing

wpf

I'm looking for an example of how to load an image from file and print it on a page using WPF. I'm having a hard time finding good information about WPF printing.

like image 345
Jon Kruger Avatar asked Nov 05 '08 12:11

Jon Kruger


People also ask

What is the use of image in WPF?

The Image class in C# represents an image control in WPF that is used to load and display an image. The Image control displays .bmp, .gif, .ico, .jpg, .png, .wdp and .tiff files. If a file is a multiframe image, only the first frame is displayed.

How to view a file in WPF Viewer?

Click on the Browse button to browse the files and the selected file will be displayed in the Viewer. Figure 2. How to view an Image in WPF? The Image element in XAML represents a WPF Image control and is used to display images in WPF.

How to load a bitmap image using Uri in WPF?

In WPF an image is typically loaded from a Stream or an Uri. BitmapImage supports both and an Uri can even be passed as constructor argument: var uri = new Uri ("http://..."); var bitmap = new BitmapImage (uri); If the image file is located in a local folder, you would have to use a file:// Uri.

How to load picture inside WPF window using btnload () function?

Double click on btnLoad and you’ll see private void btnLoad_Click (object sender, RoutedEventArgs e). Add the code below inside that function. Now, let’s test the project, run the project (press F5 button), click Load button and chose a picture you like. And you’ll see the picture inside your WPF window.


1 Answers

var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.UriSource = new Uri("");
bi.EndInit();

var vis = new DrawingVisual();
using (var dc = vis.RenderOpen())
{
    dc.DrawImage(bi, new Rect { Width = bi.Width, Height = bi.Height });
}

var pdialog = new PrintDialog();
if (pdialog.ShowDialog() == true)
{
    pdialog.PrintVisual(vis, "My Image");
}
like image 61
Tamir Avatar answered Sep 22 '22 03:09

Tamir