Kostas
Stefaniya,
That worked fine.
Thanks, Konstantinos.
Hi,
The reason for this exception is that you close the memory stream. You could try the following which will allow you to use Jpeg format with memory stream:
Infragistics.Excel.Workbook MyWorkbook = new Infragistics.Excel.Workbook(Infragistics.Excel.WorkbookFormat.Excel97To2003);
Infragistics.Excel.Worksheet ChartWorksheet = MyWorkbook.Worksheets.Add("Sheet1");
using (MemoryStream MemStream = new MemoryStream())
{
ultraChart1.SaveTo(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg);
MemStream.Flush();
Image ChartImage = Image.FromStream(MemStream);
WorksheetImage ChartShape = new WorksheetImage(ChartImage);
ChartShape.TopLeftCornerCell = ChartWorksheet.Rows[0].Cells[0];
ChartShape.TopLeftCornerPosition = new PointF(0.0f, 0.0f);
ChartShape.BottomRightCornerCell = ChartWorksheet.Rows[30].Cells[30];
ChartShape.BottomRightCornerPosition = new PointF(100.0f, 100.0f);
ChartWorksheet.Shapes.Add(ChartShape);
MyWorkbook.Save("test.xls");
}
Let me know if this works for you.
Regards,
Stefaniya
No, this did not work, same exception.What I found out though is that when I export the chart with a Png image format by calling:MyChart.SaveTo(MemStream, System.Drawing.Imaging.ImageFormat.Png);instead of MyChart.SaveTo(MemStream, System.Drawing.Imaging.ImageFormat.Jpeg); the export succeeds.
Actually the Png image format is the only one that succeeds. Maybe this has something to do with the image having transparent items like legends ect.
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.