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.
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.
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.
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.
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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With