Excel Images

Following sample shows how to add images in different formats to ExcelWorksheet using various types of positioning.

Screenshot

Excel Images Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using GemBox.Spreadsheet;
2using System;
3using System.IO;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
12
13        ExcelFile ef = new ExcelFile();
14        ExcelWorksheet ws = ef.Worksheets.Add("Images");
15
16        string pathToResources = "Resources";
17
18        ws.Cells[0, 0].Value = "Image examples:";
19
20        // Small BMP added by using rectangle.
21        ws.Pictures.Add(Path.Combine(pathToResources, "SmallImage.bmp"), 50, 50, 48, 48, LengthUnit.Pixel);
22
23        // Large JPG added by using one anchor.
24        ws.Pictures.Add(Path.Combine(pathToResources, "FragonardReader.jpg"), "B9");
25
26        // PNG added by using two anchors.
27        ws.Pictures.Add(Path.Combine(pathToResources, "Dices.png"), "J16", "K20");
28
29        // GIF added by using anchors. Notice that animation is lost in MS Excel.
30        ws.Pictures.Add(Path.Combine(pathToResources, "Zahnrad.gif"),
31                    new AnchorCell(ws.Columns[9], ws.Rows[21], 100000, 100000),
32                    new AnchorCell(ws.Columns[10], ws.Rows[23], 50000, 50000)).Position.Mode = PositioningMode.Move;
33
34        // WMF added by using one anchor and size.
35        ws.Pictures.Add(Path.Combine(pathToResources, "Graphics1.wmf"), "J9", 250, 100, LengthUnit.Pixel).Position.Mode = PositioningMode.FreeFloating;
36
37        ef.Save("Images.xlsx");
38    }
39}
1Imports GemBox.Spreadsheet
2Imports System.IO
3
4Module Samples
5
6    Sub Main()
7
8        ' If using Professional version, put your serial key below.
9        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
10
11        Dim ef As ExcelFile = New ExcelFile
12        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Images")
13
14        Dim pathToResources As String = "Resources"
15
16        ws.Cells(0, 0).Value = "Image examples:"
17
18        ' Small BMP added by using rectangle.
19        ws.Pictures.Add(Path.Combine(pathToResources, "SmallImage.bmp"), 50, 50, 48, 48, LengthUnit.Pixel)
20
21        ' Large JPG added by using one anchor.
22        ws.Pictures.Add(Path.Combine(pathToResources, "FragonardReader.jpg"), "B9")
23
24        ' PNG added by using two anchors.
25        ws.Pictures.Add(Path.Combine(pathToResources, "Dices.png"), "J16", "K20")
26
27        ' GIF added by using anchors. Notice that animation is lost in MS Excel.
28        ws.Pictures.Add(Path.Combine(pathToResources, "Zahnrad.gif"),
29                        New AnchorCell(ws.Columns(9), ws.Rows(21), 100000, 100000),
30                        New AnchorCell(ws.Columns(10), ws.Rows(23), 50000, 50000)).Position.Mode = PositioningMode.Move
31
32        ' WMF added by using one anchor and size.
33        ws.Pictures.Add(Path.Combine(pathToResources, "Graphics1.wmf"), "J9", 250, 100, LengthUnit.Pixel).Position.Mode = PositioningMode.FreeFloating
34
35        ef.Save("Images.xlsx")
36
37    End Sub
38
39End Module

Check next sample or find out more about GemBox.Spreadsheet and GemBox Software.