MS chart in crystal reports
Is is possible to include the 开发者_C百科chart image of ms chart asp.net control in crystral reports rendered in a resolution suitable for print?
My website shows an mschart and i want that same chart in my crystal report so the client can print it. I don't want to use the crystal reports chart.
I'm doing something similar - creating a chart with ms chart, rendering it on a webpage, along with other html markup (data tables), and then the user can export to PDF or Excel. To do this, and retain the same chart image, my code saves the chart on the server as a .png image, then if the user wants to export the page, my code is calling that image in the export document (either pdf or excel) and displaying it with an tag.
This is the relevant code that does the save of the chart to the server:
// save the finished image to a folder, so the PDF can retrive it later
v_chart.SaveImage(context.Server.MapPath(String.Concat(@"charts\chartimage_", v_runtimeReportKey, "_", v_chartID, ".png")));
// output the finished image to the browser
context.Response.Clear();
context.Response.ContentType = "image/png";
// following works fine with IIS 7 (Windows 7, or 2008 Server)
//v_chart.SaveImage(context.Response.OutputStream, ChartImageFormat.Png);
// must use the following for IIS 6 (Windows 2003 Server)
using(MemoryStream v_stream = new MemoryStream()){
v_chart.SaveImage(v_stream, ChartImageFormat.Png);
v_stream.WriteTo(context.Response.OutputStream);
}
When you save an MS chart as an image it always comes out at 96dpi, which isn't really great if you want to print it - should be more like 300dpi.
A simple workaround is to temporarily increase the chart size before calling SaveImage()
.
var originalWidth = myChart.Width;
var originalHeight = myChart.Height;
myChart.Width *= 3;
myChart.Height *= 3;
myChart.SaveImage("path\to\imageFile", ChartImageFormat.Bmp);
myChart.Width = originalWidth;
myChart.Height = originalHeight;
This results in an image 3 times the size of the original. When shrunk to 1/3 of its size, it will have 96 x 3 = 288dpi, which looks a lot nicer when printed.
精彩评论