Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the BackImage of a ChartArea at run time?

Anyone know why the ChartArea.BackImage is a property of type string? Would it not have made more sense for this to have been a property of type Image?

Or to put it another way, how can I set the background image of a ChartArea to an image generated at run-time (e.g. from GraphicPath objects)?

Suggestions are welcome, thanks.

like image 333
Ben Avatar asked Aug 25 '11 20:08

Ben


2 Answers

As MSDN states here: ChartArea.BackImage Property. The BackImage property is a string value that represents the URL of an image file.

So to provide an image created dynamically you will need to create your image (checkout the following article about using objects from the System.Drawing namespace to achieve this): Dynamic Image Generation with ASP.Net

Which you can either:

1 - Store to the file system.

Or

2 - Setup an HttpHandler to serve it dynamically.

See: Using HttpHandlers to serve image files

Either way you would need to set the path to the image like so:

Chart.ChartAreas[0].BackImage = imagePath;

Hope this helps.

like image 154
jdavies Avatar answered Nov 10 '22 15:11

jdavies


Solution using NamedImage

var chartBackImage = new Bitmap(1,1); //some bmp
chart1.Images.Add(new NamedImage("GiveSomeName", chartBackImage));
chart1.Areas[0].BackImage = Images[0].Name;
chart1.Areas[0].BackImageWrapMode = ChartImageWrapMode.Scaled; //extra
like image 3
Pedro77 Avatar answered Nov 10 '22 15:11

Pedro77