?
Solved

Export mschart to file

Posted on 2011-02-24
14
Medium Priority
?
2,934 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
Industry Leaders: 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!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

771 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