How to excel chart in C# picturebox

The presented C# program showcases how to retrieve the Excel chart image and display it within a Picture Box control. By utilizing the Microsoft Excel 12.0 Object Library, the provided source code demonstrates the necessary steps to obtain the chart image from Excel and seamlessly incorporate it into a Picture Box. As discussed in the previous section, we covered the procedure for importing the Microsoft Excel 12.0 Object Library into the C# project, facilitating the integration of Excel-related functionalities. Employing the outlined C# source code, developers can effectively retrieve and display Excel chart images within a Picture Box, offering enhanced visualization and presentation capabilities.

Create Excel file from CSharp

Prior to creating a chart, it is imperative to populate the Excel sheet with the necessary data. In this regard, we employ a C# program to efficiently fill the Excel sheet with the required information. Once the data has been successfully populated in the Excel sheet, it will visually resemble the provided picture, thus presenting a comprehensive and organized representation of the data.


Once the data has been successfully filled in the Excel sheet using a C# program, the subsequent step involves creating a chart object through another C# program. This chart object needs to be configured with essential information such as positions, size, data range, chart type, and more. Following the configuration, the chart can be exported as a picture file using the appropriate command. Finally, the resulting picture file can be loaded from its designated path and displayed within a picture box, thereby providing a visual representation of the charted data. By implementing these steps, you can effectively generate a chart, export it as a picture file, and seamlessly display it within a picture box using C#.

chartPage.Export(@"C:\excel_chart_export.bmp", "BMP", misValue); pictureBox1.Image = new Bitmap(@"C:\excel_chart_export.bmp");

The following picture shows the program screen after drawing the picture.

csharp-excel-chart-picturebox Full Source C#
using System; using System.Drawing; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //add data xlWorkSheet.Cells[1, 1] = ""; xlWorkSheet.Cells[1, 2] = "Student1"; xlWorkSheet.Cells[1, 3] = "Student2"; xlWorkSheet.Cells[1, 4] = "Student3"; xlWorkSheet.Cells[2, 1] = "Term1"; xlWorkSheet.Cells[2, 2] = "80"; xlWorkSheet.Cells[2, 3] = "65"; xlWorkSheet.Cells[2, 4] = "45"; xlWorkSheet.Cells[3, 1] = "Term2"; xlWorkSheet.Cells[3, 2] = "78"; xlWorkSheet.Cells[3, 3] = "72"; xlWorkSheet.Cells[3, 4] = "60"; xlWorkSheet.Cells[4, 1] = "Term3"; xlWorkSheet.Cells[4, 2] = "82"; xlWorkSheet.Cells[4, 3] = "80"; xlWorkSheet.Cells[4, 4] = "65"; xlWorkSheet.Cells[5, 1] = "Term4"; xlWorkSheet.Cells[5, 2] = "75"; xlWorkSheet.Cells[5, 3] = "82"; xlWorkSheet.Cells[5, 4] = "68"; Excel.Range chartRange ; Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250); Excel.Chart chartPage = myChart.Chart; chartRange = xlWorkSheet.get_Range("A1", "d5"); chartPage.SetSourceData(chartRange, misValue); chartPage.ChartType = Excel.XlChartType.xlColumnClustered; //export chart as picture file chartPage.Export(@"C:\excel_chart_export.bmp", "BMP", misValue); //load picture to picturebox pictureBox1.Image = new Bitmap(@"C:\excel_chart_export.bmp"); xlWorkBook.SaveAs("", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls"); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } } }