2

How can I define the image resolution (in DPI) for images created by the microsoft chart controls for .net (for the creation of .png-images).

The winforms version of the charting control has the Chart.RenderingDpi[X|Y]- property, however for the asp.net control, I can not find such a property.

Can someone lead me to a solution for doing this?

Update
During searching for a solution, I have seen that the chart-control has a Paint-method. With this I was able to create images with other DPI-settings. I'm not sure if this is the correct way to go, but the result looks not to bad to me. I have posted the code as an answer. If anyone has a more neat solution, please let me know.

henrywright
  • 10,070
  • 23
  • 89
  • 150
HCL
  • 36,053
  • 27
  • 163
  • 213

2 Answers2

5

Here a solution I have found that produces good results.

Bitmap bmp = new Bitmap(size.Width, size.Height);
bmp.SetResolution(resX,resY);
using (Graphics g = Graphics.FromImage(bmp)) {
     chart.Paint(g,new Rectangle(new Point(0,0),GetSizeOrDefault(context)));
}
HCL
  • 36,053
  • 27
  • 163
  • 213
0

If you rephrase your question as "how to get around annoying jpeg raster artifacts" I output mine as a png using the GetBytes method. I then use the constructor to set height/width.

Chart chart = new Chart(width: 1200, height: 600, theme: ChartTheme.Blue);
...
chart.GetBytes("png");

if you are curious I use it this way in a .net MVC extension method and base 64 encode into a image

return "data:image/png;base64," + System.Convert.ToBase64String(chart.GetBytes("png"));
Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33