?
Solved

Export mschart to file

Posted on 2011-02-24
14
Medium Priority
?
2,961 Views
Last Modified: 2013-12-17
Hi Experts,

I'm trying to export an mschart to a file, excel would be preferable but an image file is also ok... I found the follwing code but having problems in my web application:
protected void exportButton_Click(object sender, ImageClickEventArgs e)
        {
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}-{1}.png"));
            Chart4.SaveImage(Response.OutputStream, ChartImageFormat.Png);         
            Response.End();
         }

Open in new window

I get the error:
"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."

Can anyone explain this to me and how to resolve?
0
Comment
Question by:madkrell
  • 6
  • 3
10 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34974822
0
 

Author Comment

by:madkrell
ID: 34975054
rawinnlnx9,

Thanks but I've already looked at this thread and it doesn't address my issue?
0
 

Author Comment

by:madkrell
ID: 34975760
I've amended to:
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=filename.png"));
            Chart4.SaveImage(Response.OutputStream, ChartImageFormat.Png);     

Open in new window

but getting the error:
"A generic error occurred in GDI+"

any ideas?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 10

Accepted Solution

by:
kdwood earned 2000 total points
ID: 35039957
Greetings Madkrell,

A couple of questions:

1.  What version of Visual Studio and Chart Controls are you using?

2.  What happens if you strip your code down just to the following . . . does the chart save to an image?
       
       {
            Chart4.SaveImage("C:\SavedChart.png", ChartImageFormat.Png);        
            Response.End();
        }


Regards,

Keith

0
 
LVL 10

Expert Comment

by:kdwood
ID: 35040194

After looking at your code further, I think this section from your Response.AddHeader statement is your problem:

filename={0}-{1}.png"));

Try changing your code as follows:

 Response.AddHeader("Content-Disposition", "attachment; filename=test.png");

       {
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=test.png");
            Chart4.SaveImage(Response.OutputStream, ChartImageFormat.Png);        
            Response.End();
         }


This should pop up a save dialog with the default filename of "test.png".   Then the user can change the filename to whatever they want.   I'm not sure if this was the approach you were looking for.  Please clarify further if I am on the wrong track.

Keith

0
 

Author Comment

by:madkrell
ID: 35044361
Hi Keith,

Thanks for your help...  If you check my comment from 24/02/11 04:14 PM I'd already changed my code but getting the error

"A generic error occurred in GDI+" on the line:  
Chart4.SaveImage(Response.OutputStream, ChartImageFormat.Png); 

Open in new window

This doesn't tell me much and I can't determine problem from stack trace either:
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)
   at System.Web.UI.DataVisualization.Charting.Chart.SaveImage(Stream imageStream, ChartImageFormat format)
   at SupplierPortal.WebForm3.exportBtn_Click(Object sender, ImageClickEventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\SupplierPortal\SupplierPortal\Advanced\Analysis.aspx.cs:line 78
   at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
   at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Open in new window



Any ideas why I would be getting this error?


0
 

Author Comment

by:madkrell
ID: 35044441
Sorry I didn't answer your 1st question.  I am using VS2010.   When I use a hard coded path instead of Response.OutputStream() it pops up the open/save file dialogue box and works fine so I assume the problem GDI+ error is related to using Response.OutputStream()?
The following works ok but I want the end user to select where they save it:
 Response.AddHeader("Content-Disposition", "attachment; filename=SupplierChart.png");
            {
            Chart4.SaveImage("C:/SupplierChart.png", ChartImageFormat.Png);        
            Response.End();
            }

Open in new window

0
 

Assisted Solution

by:madkrell
madkrell earned 0 total points
ID: 35045238
Hi Keith,

Thanks for pointing me in the right direction.  From stripping down the code and looking at using Response.OutputStream() it led me to this link:
http://www.west-wind.com/weblog/posts/6008.aspx

Looks like the issue is with Jpeg type so the work around with passing to a memorystream first works as per the code below:
 
MemoryStream MemStream = new MemoryStream();
            {
                Response.ContentType = "image/png";
                Response.AddHeader("Content-Disposition", "attachment; filename= SupplierCharts.Png");
                varChart.SaveImage(MemStream, ChartImageFormat.Png);
                MemStream.WriteTo(Response.OutputStream);
                Response.End();
            }

Open in new window

0
 
LVL 10

Expert Comment

by:kdwood
ID: 35046370
Glad I could help Madkrell.  

Regards,

Keith
0
 

Author Closing Comment

by:madkrell
ID: 35106989
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Screencast - Getting to Know the Pipeline

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question