Jump to content
Software FX Community

Export chart to MemoryStream


Ramunas

Recommended Posts

HiI try to export chart image to MemoryStream object, store it to cache and show image via HTTP handler from cache. I don't know what is wrong with my code and/or chart settings but sometimes I get chart image with a missing bottom part at height 10-20 px. Moreover, if I export chart in JPEG format then both IE7 and FF shows image as it is (even with "missing" bottom part), but if I use PNG format while exporting then IE7 does not show image at all.Here is my code:chart.aspx.cs


public void RenderChart()

{

Chart chart = new Chart();

chart.DataSource = GetFormattedDataSource(); // just a simple datatable with data

chart.DataBind();

string chartID = Guid.NewGuid().ToString(); // used to store chart's image in cache

MemoryStream ms = new MemoryStream();

chart.ImgHeight = 600;

chart.ImgWidth = 1300;

chart.SerLegBox = true;

chart.DataType[0] = SoftwareFX.ChartFX.DataType.Label;

// I setup series here (Gallery, Pallete, etc.)

chart.RecalcScale(); //just in case

chart.Grid = ChartGrid.Horz;

chart.AxisX.LabelAngle = -90;

chart.Series[0].PointLabels = true;

chart.AxisY.LabelsFormat.CustomFormat = "#,###";

chart.Export(FileFormat.Jpeg, ms);

ms.Position = 0; // have read in forums that could be usefull

//store byte[] array from MemoryStream to cache

Cache.Add(chartID, ms.ToArray(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5), System.Web.Caching.CacheItemPriority.NotRemovable, null);

ms.Dispose();

// set image control's ImageUrl property to a handler to return image from cache

this.imageChart.ImageUrl = "~/Charts/GetChart.ashx?ChartID=" + chartID;

}GetChart.ashx


private const string QUERY_STRING_KEY = "ChartID";

public void ProcessRequest(HttpContext context)

{

if (string.IsNullOrEmpty(context.Request.QueryString[QUERY_STRING_KEY]))

{

return;

}

string chartID = context.Request.QueryString[QUERY_STRING_KEY];

 

byte[] chart;

chart = (byte[])context.Cache.Get(chartID);

 

context.Response.ClearHeaders();

context.Response.ClearContent();

 

context.Response.ContentType = "image/jpeg";

context.Response.Buffer = true;

context.Response.StatusCode = 200;

context.Response.OutputStream.Write(chart, 0, chart.GetUpperBound(0));

context.Response.Flush();

context.Response.End();

}I just don't know if/what I am doing wrong that I get (i suspect) corrupted images.Thanks in advanceRamunas

Link to comment
Share on other sites

Never mind. I found a bug.In case someone else has the same problem....I miss-interpreted the context.Response.OutputStream.Write() method. Last argument is not last item of array but count.So in GetChart.ashx handler instead of callingcontext.Response.OutputStream.Write(chart, 0, chart.GetUpperBound(0));I called context.Response.OutputStream.Write(chart, 0, chart.GetUpperBound(0) + 1);and all image formats (jpeg, png) are shown on page correctly.

Link to comment
Share on other sites

  • 4 years later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...