0

I have a DVC Chart displaying data. I have created a button to export the graph.

Here is the code:

private void btnExptGraph_Click(object sender, RoutedEventArgs e)
{
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)mcChart.ActualWidth, (int)mcChart.ActualHeight,96d, 96d, PixelFormats.Pbgra32);
    renderBitmap.Render(mcChart);

    Console.WriteLine(renderBitmap.ToString());

    //JpegBitmapEncoder encode = new JpegBitmapEncoder();
    PngBitmapEncoder encode = new PngBitmapEncoder();
    encode.Frames.Add(BitmapFrame.Create(renderBitmap));

    string filename = "test.bmp";

    FileStream fout = new FileStream(filename, FileMode.Create);

    encode.Save(fout);

    MessageBox.Show("File Saved Successfully");
    fout.Close();
}

So my issue is that this executes properly with the exception that my file is basically empty. I only have a file of 1KB in size and no graph.

I've looked at the MSDN documentation and other stack overflow examples. All of them follow this form and people claim that it works. I've run with a debugger and the renderBitmap object is getting the correct height and weight values in all necessary properties. Any ideas?

Noam M
  • 3,156
  • 5
  • 26
  • 41
SDSMTKyzer
  • 112
  • 10
  • Just to make it clear, JpegBitmapEncoder is also not producing the desired result? – Clemens Apr 13 '16 at 07:11
  • Are `mcChart.ActualWidth` and `mcChart.ActualHeight` greater than `0`? Your chart control either needs to be actually visible on screen to be able to render it to a `RenderTargetBitmap` or you have to call `Measure()` and `Arrange()` on it. – bitbonk Apr 13 '16 at 07:50
  • You are correct, JpegBitmapEncoder is not producing the desired result as well. I am getting a file of 2KB out of that but I believe that is simply because jpeg files are bigger anyhow. And yes mcChart.ActualWidth and mcChart.actualHeight are greater than 0. They are around 200 and 300 respectively. By visible on the screen, I assume you mean that I can see the graph? If so, yes. – SDSMTKyzer Apr 13 '16 at 13:03

1 Answers1

0

My friend took a look at it and this was the solution that we came up with and does work:

        Size size = new Size(mcChart.ActualWidth, mcChart.ActualHeight);
        if (size.IsEmpty)
            return;

        size.Height *= 2;
        size.Width *= 2;

        RenderTargetBitmap result = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Pbgra32);

        DrawingVisual drawingvisual = new DrawingVisual();
        using (DrawingContext context = drawingvisual.RenderOpen())
        {
            context.DrawRectangle(new VisualBrush(mcChart), null, new Rect(new Point(), size));
            context.Close();
        }
        result.Render(drawingvisual);

        string filename = "test.png";
        FileStream fout = new FileStream(filename, FileMode.Create);

        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(result));

        encoder.Save(fout);

        fout.Close();
        MessageBox.Show("Done");
SDSMTKyzer
  • 112
  • 10