Printing a WPF Visual Containing Images - c#

I am trying to print a WPF visual (a Canvas to be exact) containing multiple images. The image sources are loaded from base64 strings that are converted to BitmapSources. When the canvas is shown in a Window the 2 images are displayed correctly, however when its printed using PrintVisual method of PrintDialog, the images both appear the same. I have created a scaled down example that exhibits the behavior that I am seeing.
Here is the XAML I'm using:
<Window x:Class="ImagePrintTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ImagePrintTest"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="600">
<StackPanel>
<Button Content="Print" Click="Button_Click" />
<Canvas x:Name="ImageCanvas">
<Image x:Name="ImageA" Canvas.Left="50" Canvas.Top="50" Width="380" Height="56" />
<Image x:Name="ImageB" Canvas.Left="50" Canvas.Top="150" Width="380" Height="56" />
</Canvas>
</StackPanel>
And this is the code behind:
public partial class MainWindow : Window
{
string ABC_DATA = #"iVBORw0KGgoAAAANSUhEUgAAAXwAAAA4CAYAAAD+WUMEAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAxCSURBVHhe7d15cBPXHQfw7+q+JcuSbfm2aWywwRwmlHhCIAdtSpq0SSkz7aQJaabJH50m7TTptE0n+Seh02lnaKczaZv0SNp0kskxjXMMJENJORuOYBtsA8YHsi1fknVZklfn61tLBBsMCPA/ZX8fzxu0u5JWfzy++/a93bcCYwyEEEJufIrcv4QQQm5wFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITFPiEECITNB8+mUOMnWE9kVGcFuN8SWoPOLDYUYNqg0Uwz7wjD+kwYtMDbFfQh3gmk1s5P6VCD7OpCatNRhSolEJudZ6k756Ge7KD9U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DTwZfyNl7qMzEQCb4Kq3HBxT7B+YA518fYwlrvYcVS8PUdV+vgy9KXb/giogfj1AphU2dqhUzcRC5qot/G2lMTVzxMlXyMFHRtCMeyFPq7Y3iqx8H5HgvDaQODcaUc2XTNSmLF1Ao35PAQl9fvjhrYGVFo+F4t+VSxN2vj5SEb+2Jq9Tw3daZWOD6UwpmhOF7MArtbmUaCaAtj4IfNnItPJkv6uyNl/GXax7w0Nb4HKKQiJnZ0muiTVO51ZMlFcTxn4ctZQ607j/2yjU6PvCwewdF8FBcOOziWNNC18hJQBj7R/+CQTthIUFtSTQ+YCnn5sbkdwEA+5eDccAo/eCKNX30upX64x1Ev5AwlxTkRtTlW+GHjeVJge3h3wtPXixr3pKmZCr8zZmJft43tETlgtHqpfdXDRMHT8h5wu6Ixt9TcNMtET8LEiS55tJVaNYuD56PoVvHupKdHCxoz0tSTkfeetvGIbJzhrQIUMgx8IqKQ4AdxIqKQYOATEYUEA5+IKCQY+EREIcHAJyIKCQY+EVFIMPCJiEKCgU9EFArAfwE2JuRnUGyVRgAAAABJRU5ErkJggg==";
public MainWindow()
{
InitializeComponent();
ImageA.Source = Convert(ABC_DATA);
ImageB.Source = Convert(DEF_DATA);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(ImageCanvas, "image print test");
}
}
public static BitmapSource Convert(string s)
{
byte[] bytes;
bytes = System.Convert.FromBase64String(s);
BitmapSource source;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes))
{
source = PngBitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];
}
return source;
}
}
I am assuming that the image sources are being cached somehow somewhere but I really cant explain this behavior. Can anyone explain what is happening here?

It works for me when I create a second BitmapFrame like this:
public static BitmapSource Convert(string s)
{
using (var ms = new MemoryStream(System.Convert.FromBase64String(s)))
{
return BitmapFrame.Create(
BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad));
}
}
However, I don't have an explanation for this behaviour.
I also noticed that printing seems to spoil the layout. After printing, the ImageCanvas moves to position (0,0) in its StackPanel parent.

Related

How to get screenshot of all items in generated listview in wpf

I have a listview with 150 items. Each item has different background color. If I scroll down a vertical scroll bar of list view, I can separate items to three parts:
top hidden items, items is hidden on the top of listview.
displayed items.
bottom hidden items, items is hidden on the bottom of listview.
I want to save all items to image.
I try to implement base on this guide How to render a WPF UserControl to a bitmap without creating a window
MainWindow.xaml:
<Window x:Class="CaptureListEx.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CaptureListEx"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0"
Name="ListViewCtrl"
Margin="10"
BorderThickness="1"
ItemsSource="{Binding listViewItem}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
</ListView>
<Button Grid.Row="1"
Width="60"
Height="30"
Content="Capture"
Name="Capture"
Click="Capture_Click"
>
</Button>
</Grid>
</Window>
MainWindow.xaml.cs:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace CaptureListEx
{
public partial class MainWindow : Window
{
public ObservableCollection<string> listViewItem { get; set; } = new ObservableCollection<string>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
for(int i = 0; i <= 150; i++)
{
string temp = "This is item" + i;
listViewItem.Add(temp);
}
}
private void Capture_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog()
{
DefaultExt = ".jpg",
Filter = "JPG image (*.jpg)|*.jpg|All files (*.*)|*.*"
};
Nullable<bool> result = dlg.ShowDialog();
if (result == false) return;
if (File.Exists(dlg.FileName) && new FileInfo(dlg.FileName).Length != 0)
File.Delete(dlg.FileName);
double actualWidth = ListViewCtrl.ActualWidth;
ListViewCtrl.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
ListViewCtrl.Arrange(new Rect(0, 0, actualWidth, ListViewCtrl.DesiredSize.Height));
double actualHeight = ListViewCtrl.ActualHeight;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)ListViewCtrl.ActualWidth, (int)ListViewCtrl.ActualHeight, 96, 96, PixelFormats.Pbgra32);
renderTarget.Render(ListViewCtrl);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (System.IO.FileStream stream = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
encoder.Save(stream);
}
}
}
}
I expect I will get the image of all items with background color as displayed in UI, but the actual I only get image of displayed items and bottom hidden items.
Problem: Bottom hidden items do not have background color format. Top hidden items are not in image.
Could someone please help me make the right image. Thanks!
I only get image of displayed items and bottom hidden items
This happens because ListView when not limited by container dimensions, will only try to resize itself to fit as many items as it can starting from the first displayed item. In other words it doesn't try to somehow display "top hidden items" (how you call them).
I am not sure if this behavior is possible to change to make true auto-sizable ListView, but an easy workaround is to scroll to the very first item, make a screenshot and then restore position.
Add this before measuring (you will need FindChild method):
var scroll = ListViewCtrl.FindChild<ScrollViewer>();
var offset = scroll.VerticalOffset; // store offset
scroll.ScrollToTop();
Dispatcher.Invoke(() => { }, DispatcherPriority.Background); // do events
And then after render restore position:
scroll.ScrollToVerticalOffset(offset);
Why "do events"? You have to wait until wpf actually perform scrolling before measuring. As many things in wpf that one is also deferred, i.e. doesn't occurs instantly.

Drawing image into a Window and update it

I have an unsafe class that generate a Bitmap which is converted to ToImageSource in order to draw into a Window. The Bitmap itself contains a sinusoidal text which is frequently updated and I want to it "move" from the left to the right (marquee style?). Anyway it works just fine in a WinForm but I'm stuck with the WPF Window.
Here are some code samples:
public AboutWindow()
{
InheritanceBehavior = InheritanceBehavior.SkipAllNow;
InitializeComponent();
Initialize();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
drawingContext.DrawImage(bitmapRender.WindowBitmap, drawingArea);
if (!worker.IsBusy)
worker.RunWorkerAsync(); // BackgroundWorker in charge of updating the bitmap
}
void DispatcherTimerRender_Tick(object sender, EventArgs e) => InvalidateVisual();
My issues are: there is nothing displayed on the Window and the DispatchedTimer that calls InvalidateVisual() leads to this exception:
System.InvalidOperationException: 'Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.'
I have looked at other threads and I'm aware that WPF is a retained drawing system but I would love to achieve it anyway.
Any suggestion about the "best" way to achieve this?
Any useful explanation/link would be very much appreciated.
[Edit]
<Window x:Class="CustomerManagement.View.AboutWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Height="450" WindowStartupLocation="CenterScreen" Width="800" ResizeMode="NoResize" AllowsTransparency="True" WindowStyle="None">
<Grid KeyDown="Grid_KeyDown">
<Image Width="800" Height="450" Source="{Binding 'Image'}" />
</Grid>
</Window>
You should use an Image element that has its Source property bound to an ImageSource property in a view model. This is the "standard" way, based on the MVVM architectural pattern, and therefore the "best" way - in my opinion.
<Image Source="{Binding Image}"/>
The view model could look like this:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ImageSource image;
public ImageSource Image
{
get { return image; }
set
{
image = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Image)));
}
}
}
and an instance of it would be assigned to the DataContext of the window:
public AboutWindow()
{
InitializeComponent();
var vm = new ViewModel();
DataContext = vm;
}
For testing it, the code below performs a slide show of image files in a directory. You may as well assign any other ImageSource - e.g. a DrawingImage - to the Image property.
var imageFiles = Directory.GetFiles(..., "*.jpg");
var index = -1;
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
timer.Tick += (s, e) =>
{
if (++index < imageFiles.Length)
{
vm.Image = new BitmapImage(new Uri(imageFiles[index]));
}
else
{
timer.Stop();
}
};
timer.Start();

WPF binding image not working

I am trying to set up the image on a grid.
I used 2 ways to do it:
1) using image source:
<Image Source="{Binding MonitoringVM.ImgPath}"
Width="1600" Height="1600" />
When I do it this way, the image gets displayed. BUT it works only if the width/height is 500. When I set 2000 I can only see the small piece of the image. I expected it to enlarge the image and display me only the part which corresponds to the size of the grid which contains the image component, however it does not work.
2) For this approach I overwritten Canvas:
public class ImageCanvas : Canvas
{
public ImageSource CanvasImageSource
{
get { return (ImageSource)GetValue(CanvasImageSourceProperty); }
set { SetValue(CanvasImageSourceProperty, value); }
}
public static readonly DependencyProperty CanvasImageSourceProperty =
DependencyProperty.Register("CanvasImageSource", typeof(ImageSource),
typeof(ImageCanvas), new FrameworkPropertyMetadata(default(ImageSource)));
protected override void OnRender(System.Windows.Media.DrawingContext dc)
{
dc.DrawImage(CanvasImageSource, new Rect(this.RenderSize));
base.OnRender(dc);
}
}
BUT
I works fine (including size manipulation) only when I statically specify the path:
<helper:ImageCanvas CanvasImageSource="/Images/img1.png"
Width="500" Height="500" >
</helper:ImageCanvas>
And if I replace the path with the binding it does not displayme anything at all:
<helper:ImageCanvas CanvasImageSource="{Binding MonitoringVM.ImgPath}"
Width="500" Height="500" >
</helper:ImageCanvas>
The ViewModel is: ImgPath = #"/Images/img1.jpg";
Your ImageCanvas is redundant, because it behaves like an Image control with Stretch set to Fill.
So just write this:
<Image Source="{Binding MonitoringVM.ImgPath}" Stretch="Fill" Width="500" Height="500" />

how to screen capture in wpf? [duplicate]

This question already has an answer here:
Desktop screenshot in WPF
(1 answer)
Closed 6 years ago.
I want to simple screen short on click.
I create a simple project from c# windows form.
But I can not on wpf.
Here is windows form code:
using System.Drawing;
using System.Windows.Forms;
private void short_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
System.Windows.Forms.SendKeys.Send("{PRTSC}");
Image img = Clipboard.GetImage();
pictureBox1.Image = img;
img.Save("D:\\myimg.jpg"); //Its save only one picture
}
But it save only one picture .
Question for wpf:
1: How can I capture screen on click?
2: How can I save multiple picture at a time?
Note:
Just simple
Please help me anybody
Please
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr onj);
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Bitmap bitmap;
bitmap = new Bitmap((int) SystemParameters.PrimaryScreenWidth,(int) SystemParameters.PrimaryScreenHeight,PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(0,0,0,0,bitmap.Size);
}
IntPtr handle = IntPtr.Zero;
try
{
handle = bitmap.GetHbitmap();
ImageControl.Source = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
bitmap.Save("D:\\1.jpg"); //saving
}
catch (Exception)
{
}
finally
{
DeleteObject(handle);
}
}
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="1" Height="10" Click="ButtonBase_OnClick"></Button>
<Image Grid.Row="0" x:Name="ImageControl"></Image>
</Grid>
I use the following code to create a screenshot and show it on a wpf Image control. You should be able to save the bitmap as a jpeg or so
var bitmap = new Bitmap(Screen.AllScreens[SelectedScreen.Index].Bounds.Width, Screen.AllScreens[SelectedScreen.Index].Bounds.Height);
var graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(Screen.AllScreens[SelectedScreen.Index].Bounds.Left, Screen.AllScreens[SelectedScreen.Index].Bounds.Top, 0, 0, bitmap.Size);
this.Dispatcher.Invoke(() => this.PreviewImage.Source = this.ConvertBitmapToBitmapImage(bitmap));

NullReferenceException loading RenderWindowControl for vtk in WPF

I am trying to load a RenderWindowControl from vtk libraries on my WPF proyect using ActiViz.NET and Visual Studio 2013. The library works fine since I did a new project just to practice on itbut when I tried to integrate it into my work, I got a null RenderWindowControl this time. This is my code:
MainWindow.xaml:
<Window x:Class="myProject.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
x:Name="Mainwindow"
MinHeight="600"
MinWidth="800"
Title="{Binding Title}"
Height="720"
Width="1280"
Icon="{StaticResource ApplicationIcon}"
Loaded="OnLoaded"
DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
Style="{StaticResource WindowStyle}"
mc:Ignorable="d">
<DockPanel>
<TabControl>
....
....
<VtkTab:VtkTabView />
....
....
</TabControl>
</DockPanel>
</Window>
VtkTabView.xaml:
<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
Loaded="WindowLoaded"
Height="480" Width="640">
<WindowsFormsHost Name="Wfh">
<vtk:RenderWindowControl x:Name="RenderControl" />
</WindowsFormsHost>
</UserControl>
VtkTabView.xaml.cs:
public partial class UITabView
{
protected static Random _random = new Random();
vtkActor actor = vtkActor.New();
public VtkTabView()
{
InitializeComponent();
var sphere = vtkSphereSource.New();
sphere.SetThetaResolution(8);
sphere.SetPhiResolution(16);
var shrink = vtkShrinkPolyData.New();
shrink.SetInputConnection(sphere.GetOutputPort());
shrink.SetShrinkFactor(0.9);
var move = vtkTransform.New();
move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
var moveFilter = vtkTransformPolyDataFilter.New();
moveFilter.SetTransform(move);
moveFilter.SetInputConnection(shrink.GetOutputPort());
var mapper = vtkPolyDataMapper.New();
mapper.SetInputConnection(moveFilter.GetOutputPort());
// The actor links the data pipeline to the rendering subsystem
actor.SetMapper(mapper);
actor.GetProperty().SetColor(1, 0, 0);
}
private void WindowLoaded(object sender, RoutedEventArgs e)
{
var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
renderer.AddActor(actor);
}
}
RenderControl.RenderWindow is null on WindowLoaded (VtkTabView.xaml.cs) and I do not know why. Might it be because I load UITabView from a second xamp and I lose the content of RenderControl?, it is the only difference I see compare to the example I did.
Access the RenderWindow on Load event of the RenderWindowControl.
e.g.
public VtkTabView()
{
InitializeComponent();
// initialize your sphrere and actor
RenderControl.Load += MyRenderWindowControlOnLoad;
}
private void MyRenderWindowControlOnLoad(object sender_in, EventArgs eventArgs_in){
//access the RenderWindow here
}

Categories

Resources