How to Fill Chart Elements with Pictures in C#, VB.NET

A chart filled with an image of company logo or propaganda is more impressive than a plain chart; adding a proper image into a chart as background will dramatically draw attention from your readers. As is similar to MS Excel, Spire.XLS enables users to insert pictures to specific chart elements such as chart area, bar area and plot area. In this article, I'll introduce how to enhance your chart by inserting image in chart area and plot area in C#, VB.NET.

Test File:

Fill Chart Elements with Pictures

Code Snippet for Inserting Background Image:

Step 1: Create a new workbook and load the test file

Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");

Step 2: Get the first worksheet from workbook, get the first chart from worksheet.

Worksheet ws = workbook.Worksheets[0];
Chart chart = ws.Charts[0];

Step 3:

A) Insert chart area with a custom picture and set the transparency of plot area as 0.9. If you don't make plot area transparent, it will cover up the background image filled in chart area. Anyway, it all depends on your own needs.

chart.ChartArea.Fill.CustomPicture(Image.FromFile("05.jpg"), "None");
chart.PlotArea.Fill.Transparency = 0.9;

B) Insert plot area with a custom picture

chart.PlotArea.Fill.CustomPicture(Image.FromFile("01.jpg"), "None");

Step 4: Save the file

workbook.SaveToFile("Sample.xlsx",ExcelVersion.Version2010);

Result:

A) Fill chart area with image

Fill Chart Elements with Pictures

B) Fill plot area with image

Fill Chart Elements with Pictures

Full Code:

[C#]
using Spire.Xls;
using System.Drawing;
namespace FillChartElement
{
    class Program
    {

        static void Main(string[] args)
        {

            Workbook workbook = new Workbook();
            workbook.LoadFromFile("test.xlsx");

            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            // A. Fill chart area with image
            chart.ChartArea.Fill.CustomPicture(Image.FromFile("05.jpg"), "None");
            chart.PlotArea.Fill.Transparency = 0.9;

            //// B.Fill plot area with image
            //chart.PlotArea.Fill.CustomPicture(Image.FromFile("05.jpg"), "None");

            workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2010);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing
Namespace FillChartElement
	Class Program

		Private Shared Sub Main(args As String())

			Dim workbook As New Workbook()
			workbook.LoadFromFile("test.xlsx")

			Dim ws As Worksheet = workbook.Worksheets(0)
			Dim chart As Chart = ws.Charts(0)

			' A. Fill chart area with image
			chart.ChartArea.Fill.CustomPicture(Image.FromFile("05.jpg"), "None")
			chart.PlotArea.Fill.Transparency = 0.9

			'''/ B.Fill plot area with image
			'chart.PlotArea.Fill.CustomPicture(Image.FromFile("05.jpg"), "None");

			workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2010)
		End Sub
	End Class
End Namespace