Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
300
Expoting a chart image in Excel
posted
Hi,
I am using Infragistics V10.1 and i am trying to export a chart in Excel as an Image.
Here is the code i use.

Infragistics.Excel.Workbook MyWorkbook = new Infragistics.Excel.Workbook(Infragistics.Excel.WorkbookFormat.Excel97To2003);
Infragistics.Excel.Worksheet ChartWorksheet = MyWorkbook.Worksheets.Add("Sheet1");
MemStream = new MemoryStream();
MyChart.SaveTo(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg);
MemStream.Flush();
Image ChartImage = Image.FromStream(MemStream);
MemStream.Close();

WorksheetImage ChartShape = new WorksheetImage(ChartImage);
ChartShape.TopLeftCornerCell = pWorkSheet.Rows[0].Cells[0];
ChartShape.TopLeftCornerPosition = new PointF(0.0f, 0.0f);
ChartShape.BottomRightCornerCell = pWorkSheet.Rows[30].Cells[30];
ChartShape.BottomRightCornerPosition = new PointF(100.0f, 100.0f);
ChartWorksheet.Shapes.Add(ChartShape);

MyWorkbook.Save(@"c:\test.xls");

And here is the Exception i get when MyWorkbook.Save is called:
"A generic error occurred in GDI+."
When i use
MyChart..SaveTo(@"c:\test.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Image ChartImage = Image.FromFile(@"c:\test.jpeg");
instead of the MemoryStream everything works as expected.
Is this a bug or am i missing something regarding the use of the MemoryStream?

Kostas

Parents
No Data
Reply
  • 4960
    Suggested Answer
    posted

    When you save the chart image to the MemoryStream and Flush it I believe that the MemoryStream pointer is at the end of stream, so I think you need to set the MemoryStream pointer back to the beginning of stream with Seek before you call Image.FromStream( ).

    MemStream.Flush();
    MemStream.Seek(0, SeekOrigin.Begin);

    Image ChartImage = Image.FromStream(MemStream);

    Please let us know if this solution works for you.

    Regards,

Children