Solved

Export mschart to file

Posted on 2011-02-24
14
2,886 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

15 Experts available now in Live!

Get 1:1 Help Now