?
Solved

Export mschart to file

Posted on 2011-02-24
14
Medium Priority
?
2,950 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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