Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
175
RenderTargetBitmap and XamChart - Broken, Redux
posted

I don't think I was clear enough when I first asked this question. 

I'm writing an application that has need of making small bitmaps of charts for static preview purposes. I've tried everything I can think of to get RenderTargetBitmap to work with XamChart, but all I get is an empty bitmap (the bitmap is the correct size, it's just filled with transparent pixels).  The following code demonstrates the use of RenderTargetBitmap with a regular button and a XamChart. Both are created at run time and are never displayed. Creating a bitmap of the button works, but not XamChart.

Keep in mind that this must happen off-screen. I do not want to show the user a chart before creating the thumbnail, I thus cannot add it to the visual tree before using RenderTargetBitmap. I need to be able to render a bitmap of the XamChart object completely offscreen, by creating the XamChart programmatically, using RenderTargetBitmap to create the bitmap image, and never adding the XamChart to the visual tree or displaying the actual XamChart object to the user in any way.  I need to do this in the same manner in which I'm rendering the button in the code below (the button is never added to the visual tree or displayed to the user).

My question is: How does one use RenderTargetBitmap with XamChart to create a bitmap image without first adding the XamChart to the visual tree?  The code below is the simplest try I've used; I've tried a number of different techniques, including using BeginInit and EndInit, all to no avail.

 

<Window x:Class="XamChartTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="XamChartTest" Height="300" Width="300"
    >
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
     
      <Button Content="XamChart Test" Click="OnClickChart" Grid.Row="0"/>
      <Button Content="Regular Test" Click="OnClickTest" Grid.Row="1" />
    </Grid>
</Window>

 

        public void OnClickChart(object sender, RoutedEventArgs e)
        {
            XamChart testChart = new XamChart();
            testChart.Width = 200;
            testChart.Height = 200;
            testChart.Measure(new Size(200, 200));
            testChart.Arrange(new Rect(0, 0, 200, 200));

            RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(testChart);

            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(bmp));
            using (Stream stm = File.Create(@"C:\TestChart.png"))
                png.Save(stm);
        }

        public void OnClickTest(object sender, RoutedEventArgs e)
        {
            Button testButton = new Button();
            testButton.Content = "Test Button";
            testButton.Width = 200;
            testButton.Height = 200;
            testButton.Measure(new Size(200, 200));
            testButton.Arrange(new Rect(0, 0, 200, 200));

            RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(testButton);

            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(bmp));
            using (Stream stm = File.Create(@"C:\TestButton.png"))
                png.Save(stm);
        }

 

  • 28496
    Offline posted

    Andrew Smith posted this in response to the older thread on this subject ... it seems adding the chart control to the logical tree will allow it to render in your bitmap.  does this work for you? 

     

    XamChart testChart = new XamChart();
    testChart.Width = 200;
    testChart.Height = 200;
    this.AddLogicalChild(testChart);
    testChart.Measure(new Size(200, 200));
    testChart.Refresh();
    testChart.Arrange(new Rect(0, 0, 200, 200));
    testChart.UpdateLayout();
    RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);

    bmp.Render(testChart);

    PngBitmapEncoder png = new PngBitmapEncoder();
    png.Frames.Add(BitmapFrame.Create(bmp));
    using (Stream stm = File.Create(@"C:\TestChart.png"))
    {
        png.Save(stm);
    }
    this.RemoveLogicalChild(testChart);