Handling chart generated images using Chart Http Handler


When the Chart.RenderType property is set to ImageTag, the Chart control is rendered as standard image (img) tag. Displaying an image in current web browsers is a two stage process: first the browser renders the html page and then downloads the image from specified Url. Because real chart image is created during web page processing the Chart needs a storage where image will stay until the browser requests it.

You can specify how the Chart control manages the image files. To do this, use the ImageStorageMode property. By default, the ImageStorageMode property is set to UseHttpHandler. This causes the Chart control to use the ChartHttpHandler that is registered in the web.config file to manage the rendered chart images.

Use the chart HTTP handler to:


  • Prevent file override collisions in a server cluster or a multiple-process site.
  • Protect the security of rendered chart images by preventing users from downloading chart images that are rendered for other users.
  • Reduce disk operations by storing image files in memory or other storage methods, such as SQL Server.

When you drag a Chart control from the Toolbox onto the design surface of the ASP.NET page, the ChartHttpHandler is automatically registered in the web.config file as “ChartImageHandler”. You can configure ChartImageHandler’s behavior in the <appSettings> element. The applicable code section is provided below with the automatically generated settings:

<appSettings>
    
<add key=”ChartImageHandler” value=”storage=file;timeout=20;dir=c:\TempImageFiles\;” />
</appSettings>

Use the parameters in the value attribute to configure the chart HTTP handler. See the table below for a description for each parameter.


 






























Parameter Description
storage

Storage mode. Three options are available:



  • file: Store the rendered chart images as temporary files. You must also specify the storage location in the url or dir parameter. In a server cluster or a multiple-process site, you must specify the dir parameter.



  • memory: Store the rendered chart images in the memory space of the running process. Do not use this option in a server cluster or a multiple-process site.



  • session: Store the rendered chart images in a session variable.

url

The relative URL of the image storage location. If the dir parameter is specified, it is used instead of url. The default value is “~/”.

dir Absolute directory path of the image storage location. This can be either a local path or network path. In a server cluster environment, use a network path to keep the temporary files in a shared location. Each temporary file is assigned with a unique machine ID to prevent the files from being overridden inappropriately.
timeout

The timeout period in seconds for rendered chart images. After this period has elapsed for an image, it may be replaced by a newer image. The default value is 30.

handler A custom IChartStorageHandler implementation. The value should be formatted as a fully qualified assembly name. For example: MyNamespace.MyClass.MyHandler.
privateImages

Privacy protection. When set to true, the generated image can only be downloaded by its owner if some of the following types of identifications are enforced:


  • The user is authenticated.
  • AnonymousID is enabled.
  • SessionID is available.

The default value is true.

deleteAfterServicing

Whether the image should be deleted after successful download from a client. The default value is true.

webDevServerUseConfigSettings

Whether to store rendered chart images in memory when running the application in full trust level using the Visual Studio development server.

The default value is true. This may mean that your settings in the web.config file have no effect when you run the ASP.NET application from Visual Studio.



  

Different trust levels have impact on ChartHttpHandler behavior. The following is a list of trust levels and how the trust levels affect the configuration settings in the web.config file.



  • Full trust: All configuration variations apply.



  • High trust: The ASP.NET Development Server cannot be detected. Therefore, webDevServerUseConfigSettings has no effect.



  • Medium trust: Same as high trust.



  • Low trust: Same as high trust, except that file access is forbidden. The storage parameter must be set to session or memory.



  • Minimal trust: Same as low trust, except that the chart HTTP handler detection fails. This means that the ChartImageHandler key in your web.config file is mandatory.


Comments (18)

  1. Delian Tchoparinov just posted on his blog about chart image rendering using Chart HTTP Handlers. This

  2. mb35412 says:

    The chart tool is a great application, when it works.  The fact that the image not found problem is one of the most popular topics tells me that many people are having the same problem, and that it isn’t a single issue.

    We have it working fine on the test box but not on the production box. We checked all the settings (web.config, temporary asp.net folder, iis settings) are exactly the same.

    We tried the deleteAfterServicing=false option and saw that the file is being created but it is not able to get it on the web page. It gives a "image not found" error.

    The current appSetting that we are using is as follows:

    <appSettings>

           <add key="ChartImageHandler" value="storage=file;timeout=20;dir=C:WINNTMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Files;" />

       </appSettings>

    We also tried running process monitor on each of these servers. We noticed that on the test server, there are write and read requests generated for the .png file, but on the production, it only generates the write requests. We do not see any read requests.

    Also, the process that generates these requests are different on each of these servers. The test server requests are generated by the aspnet_wp.exe whereas the production server requests are generated by w3wp.exe..

    We are not sure if that is a problem and if so, how can we change the process generating the requests. Also, why would the read requests not get generated at all.

  3. KarlMC says:

    Hi,

    Bit of background I am using the MSChart Controls in a sharepoint environment that must be deployable on a farm environment.

    I am using the following url parameter to try and store the temporary images in a sharepoint:

    <add key="ChartImageHandler" value="Storage=file;Timeout=20;Url=/Images1/" />

    where /Images1/ is the relative url of the document library. i.e. the full url is http://localhost:80/Images1/.

    However, I get the following error:

    An exception occurred:System.InvalidOperationException: Invalid 'url' in chart handler configuration. —> System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) at System.Web.HttpServerUtility.MapPath(String path) at System.Web.UI.DataVisualization.Charting.ChartHttpHandlerSettings.Inspect() The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.FileIOPermission The Zone of the assembly that failed was: MyComputer — End of inner exception stack trace — at System.Web.UI.DataVisualization.Charting.ChartHttpHandlerSettings.Inspect() at System.Web.UI.DataVisualization.Charting.ChartHttpHandlerSettings.ParseParams(String parameters) at System.Web.UI.DataVisualization.Charting.ChartHttpHandlerSettings..ctor(String parameters) at System.Web.UI.DataVisualization.Charting.ChartHttpHandler.InitializeParameters() at System.Web.UI.DataVisualization.Charting.ChartHttpHandler.EnsureInitialized(Boolean hardCheck) at System.Web.UI.DataVisualization.Charting.Chart.GetImageStorageMode() at System.Web.UI.DataVisualization.Charting.Chart.Render(HtmlTextWriter writer) at ChartPart.ChartPartWebPart.Render(HtmlTextWriter writer)

    The account of my app pool has full write access to the site and to the content database.

    Any Ideas why I may be getting this error?

    Thanks in advance.

  4. Masoud says:

    mb35412

    your iis versions are different on production and test boxes. aspnet_wp is the worker process for iis 5.1 where w3wp is for iis 6 and maybe 7. i think i saw some configuration you need to apply to make it work on different versions of iis.

  5. gelbert says:

    I have implemented custom IChartStorageHandler to control temporary location for image files depending on environment (production, testing, development). How do I ensure that names on temporary image files will not conflict on  a web farm ? It looks like if you use custom IChartStorageHandler and ImageStorageMode="UseHttpHandler", name generation settings in "ImageLocation" attribute are ignored.

  6. Rakshun says:

    Use session :/

    Configure it on the load balanced stuff

  7. Rakshun says:

    Hi Rakshun

    Can you please elaborate on your comment "Configure it on the load balanced stuff". My web servers are on a load balanced environment and i am not sure what is the best way to specify "ChartImageHandler". Thank you

    Kiran

  8. Sands says:

    Try this in your web.config file:

    <appSettings>

    <add key="ChartImageHandler" value="storage=session;timeout=20;" />

    OR

    <add key="ChartImageHandler" value="storage=memory;timeout=20;" />

    </appSettings>

    Sands

  9. Ashok says:

    Hi all,

    We have used Chart control in our web application and worked fine. Now we wanted to publish website to Windows Azure cloud environment. There these Chart contrls are giving out of memory exception.

    Can anybody please give suggessions/Solution?

  10. Ashok says:

    Well, I wanted to give some context of the problem.

    We are using .net3.5 framework , the ChartImageHandler section in web.config  is

    <add key="ChartImageHandler" value="storage=session;timeout=20;" />

  11. vinayakbaddi says:

    Hi,

    I want to do a Jquery POST and get a chart using renderControl (which will return HTML attributes) I have successfully done this on WEbforms, However the same with MVC give a empty image. The handler doesn't perform the same in MVC2 3.5. I went near to 50 sites now.

    No solutions could resolve this.

    I can't use base64encoding as IE6, 7 don't support it.

    What I'm trying to do?

    I have few data parameteres to be sent (POST) to server and in response get the Chart image as <img> tag along with other attributes I will also append OTHER data all this to be done using JQUERY. No postbacks to be used.

  12. skeezmoe says:

    I am trying to use a network path for the images:

    <add key="ChartImageHandler" value="storage=file;timeout=10;dir=\servernametemp;" />

    And I keep getting this error: Invalid temp directory in chart handler configuration

    Can someone help. Please reply to cenon@optusnet.com.au

  13. spidy says:

    skeezmoe,

    You probably got your problem resolved but your dir attribute should be something like this

    dir=~/Images/Chart

    or you need to specify a full path.

    -sPidy

  14. Tuomo Kämäräinen says:

    Removing "dir" solved my issue with missing folder. It doesn't seem to be mandatory.

       <!–<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:TempImageFiles;"/>–>

       <add key="ChartImageHandler" value="storage=file;timeout=20;" />

  15. SoniSp says:

    Hi,

    I am trying to use a network path for the images as I have a WebFarm which is loadBalanced using CISCO ACE appliance. The reason I am using a network shared folder is that at times the Chart does not show the PNG images.

    The problem with displaying the PNG images in the Web Farm comes when I had initially the web.config setting as shown below.

    <add key="ChartImageHandler" value="storage=file;timeout=10;dir=c:TempImageFiles;" />

    So I used the network Shared path as per (blogs.msdn.com/…/managing-chart-generated-images-with-chart-image-handler.aspx) , where I have FULL Permissions on EVERYONE, NETWORK Service.

    Below is my current setting for the Network shared folder path and I keep getting this error: Invalid temp directory in chart handler configuration

    <add key="ChartImageHandler" value="storage=file;timeout=10;dir=\servernameTempImageFiles;" />

    Can someone help. Please reply to soni.sp@gmail.com

  16. Elias Echeverri from CondosCafe.com says:

    I store images in RAM memory as follows and I do not have any issues with overwritting or file systems. You can see the web site in action at http://www.condoscafe.com. I use a lot of charts:

     <appSettings>

       <!– Set the Chart Web control to save the chart image to memory and to free that memory as soon as the image is server –>

       <add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true; WebDevServerUseConfigSettings=true;"/>

     </appSettings>

    <system.web>

       <!–

    Needed to debug in remote server

    –>

       <customErrors mode="Off"/>

       <httpHandlers>

         <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

       </httpHandlers>

       <pages>

         <controls>

           <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

         </controls>

       </pages>

       <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">

         <assemblies>

           <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

         </assemblies>

       </compilation>

     <system.webServer>

       <defaultDocument>

         <files/>

       </defaultDocument>

       <handlers>

         <remove name="ChartImageHandler"/>

         <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

       </handlers>

     </system.webServer>

  17. Ashwini says:

    Hi,

    i m developing an application which shows reports as bar chart using the chart control..now i want to email the report…the charts appear on composed mail but the received mail doesn't contain the chart image…any help..thanks in advance…

    <appSettings>

       <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

       <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:TempImageFiles;" />

     </appSettings>

  18. ashwini says:

    Hi,i got the solution …i just needed to set deleteAfterServicing=false;