How may I get the image of DataVisualization.Chart

2019-06-25 05:35发布

问题:

I'm writing a piece of software for visualization of measurement data. For this I use System.Windows.Forms.DataVisualization.Charting.Chart and I do know that I can get the shown image by chartObj.SaveImage to store it to a file.

My software shall have a PDF export in which the picture should be included. For this, I'm using iTextSharp. Again, I do know how to put a picture which I have stored in a file into the PDF by iTextSharp.text.Image.GetInstance.

So by now I am able to take the picture of the chart, put it to a file (e.g. a .jpg file) and load this file again to put it in my PDF. Now I'm looking for a nice solution to get the picture into the PDF without storing it into a file, maybe through a Stream or something like that. I've tried quite some time, but until now I didn't succeed. I've thought of something like

Stream imageStream = image of chartObj;
iTextSharp.text.Image picture = iTextSharp.text.Image.GetInstance(imageStream);

As far as I understand, I fail in putting the picture from the chartObj into a Stream instead of a file. If I had this, I guess I could load the Stream via iTextSharp.text.Image.GetInstance.

Has anyone some help you could offer? Guess it's not that difficult, but I'm new to C# and also to iText, so I'm just a bit stucked here.

Thanks in advance for every thought you have about this!

Anna

回答1:

SaveImage to a MemoryStream:

using (var chartimage = new MemoryStream())
  {
    chart.SaveImage(chartimage, ChartImageFormat.Png);
    return chartimage.GetBuffer();
  }

From: Microsoft Chart Controls to PDF with iTextSharp and ASP.NET MVC