Solved

Export mschart to file

Posted on 2011-02-24
14
2,882 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
14 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
 
LVL 10

Accepted Solution

by:
kdwood earned 500 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now