05-07-2022 02:51 AM
Hi Experts,
Recently we have a request to create screenshots of waveforms, we implement this with WPF RenderTargetBitmap to save the graph grid to a image file. Since we have a lot of waveforms to save, so in a loop, we set the DataSource of the viewmodel each time and show the window, then in the window's Window_ContentRendered event to save the grap image. But the problem is that, when the data source is large, for example, 4 channels with 1M ponits of each channel, some of the generated image file lost the plot area. See the attached pictures.
Here is the code we use the graph:
<ni:Graph Name="graph" HorizontalAlignment="Stretch" Margin="0" DataSource="{Binding DataSource}" VerticalAlignment="Stretch" Cursor="Arrow" DataProcessed="graph_DataProcessed">
<ni:Graph.Axes>
<ni:AxisDouble x:Name="horizontalAxis" Orientation="Horizontal" Range="{Binding Xrange,Mode=TwoWay}" Adjuster="None">
</ni:AxisDouble>
</ni:Graph.Axes>
</ni:Graph>
And here is how we handle the Window_ContentRendered event:
private void Window_ContentRendered(object sender, EventArgs e)
{
double width = Convert.ToInt32(this.MainGrid.ActualWidth);
double height = Convert.ToInt32(this.MainGrid.ActualHeight);
double dpi = 300;
double scale = dpi / 96;
this.Dispatcher.BeginInvoke(new Action(() =>
{
var rtb = new RenderTargetBitmap((int)(width * scale), (int)(height * scale), dpi, dpi, System.Windows.Media.PixelFormats.Default);
rtb.Render(this.MainGrid);
var bit = ImageHandler.BitmapSourceToBitmap(rtb);
bit.Save(_screenShotFile);
bit.Dispose();
this.Close();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}),DispatcherPriority.ContextIdle,null);
}
Usually, when the datasource is 4*1M, we have about 10 images lost every 100 pictures, if the data source is 4*100k, only few lost images. But on some old machines, even 4*100k will also lost about 20% images.
According to my understanding, when the window_contentRendered event is triggered, the NI Graph should already complete rendering the image, I don't know why we lost the waveforms when saving to image file. Is there any other event I should use to save the WPF Grid as a image? Thanks in advance.
Solved! Go to Solution.
05-07-2022 09:39 AM
I won't have a chance to investigate until next week, but you might try disabling background processing to ensure the graph displays all values synchronously:
<ni:Graph
niPrimitives:GraphConfiguration.UseBackgroundProcessing="False"
niPrimitives:GraphConfiguration.UseBackgroundRendering="False"
...
Alternatively, you might want to wait until both Window_ContentRendered
and graph_DataProcessed
events have fired before trying to save the image.
05-09-2022 09:59 PM
Hi Paul,
Many thanks for your reply, I will try it tomorrow when I have the online machine.
05-12-2022 04:28 AM
It works, thanks a lot, you saved me.