[]
Images can be added to a sheet or cell using one of the following methods.
Using this method, the image is added to the sheet and kept at its original size. The upper left corner of the image coincides with the upper left corner of the specified cell.
Load an existing workbook or add some content to a new workbook.
Dim wb As New C1XLBook
wb.Load("C:\Project\WorkBook1.xls")
C1XLBook wb = new C1XLBook();
wb.Load(@"C:\Project\WorkBook1.xls");
Specify the image and assign it to the cell's Value property.
Dim img As WriteableBitmap = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
sheet(0, 0).Value = img
WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
XLSheet sheet = wb.Sheets["Forecasting Report"];
sheet[0,0].Value = img;
Save and open the new book:
wb.Save("C:\Project\WorkBook1.xls ")
System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
wb.Save(@"C:\Project\WorkBook1.xls");
System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
In this example, the image replaces the value in the first cell, and it appears at its original size in the first cell.
This second method allows you to customize the image by specifying its size, rotation angle, brightness, contrast, border, and more.
Load an existing workbook or add some content to a new workbook.
Dim wb As New C1XLBook
wb.Load("C:\Project\WorkBook1.xls")
C1XLBook wb = new C1XLBook();
wb.Load(@"C:\Project\WorkBook1.xls");
Create an XLPictureShape object, set some of its properties and assign it to a cell's Value property.
Dim img As WriteableBitmap
= new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
Dim pic As New XLPictureShape(img, 1500, 1500)
pic.Rotation = 30.0F
pic.LineColor = Color.DarkRed
pic.LineWidth = 100
' assign the pic to the first cell of the specified sheet
Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
sheet(0, 0).Value = pic
WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
XLPictureShape pic = new XLPictureShape(img, 1500, 1500);
pic.Rotation = 30.0f;
pic.LineColor = Color.DarkRed;
pic.LineWidth = 100;
// assign the pic to the first cell of the specified sheet
XLSheet sheet = wb.Sheets("Forecasting Report");
sheet[0,0].Value = pic;
Save and open the book.
wb.Save("C:\Project\WorkBook1.xls ")
System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
wb.Save(@"C:\Project\WorkBook1.xls");
System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
In this example, the image replaces the value in the first cell, is rotated 30°, and has a dark red border. Since we have specified the horizontal and vertical position of the image, it does not appear in the first cell.
This method uses the XLPictureShape constructor to specify the image boundaries in sheet coordinates. The shape is added directly to the sheet's ShapeCollection, rather than to a specific cell.
Load an existing workbook or add some content to a new workbook.
Dim wb As New C1XLBook
wb.Load("C:\Project\WorkBook1.xls")
C1XLBook wb = new C1XLBook();
wb.Load(@"C:\Project\WorkBook1.xls");
Create an XLPictureShape object, set some of its properties and assign it to a sheet's ShapeCollection.
Dim img As WriteableBitmap
= new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
Dim pic As New XLPictureShape(img, 3000, 3500, 2500, 900)
pic.Rotation = 30.0F
pic.LineColor = Color.DarkRed
pic.LineWidth = 100
' add the pic to specified sheet's ShapeCollection
Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
sheet.Shapes.Add(pic)
WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
XLPictureShape pic = new XLPictureShape(img, 3000, 3500, 2500, 900);
pic.Rotation = 30.0f;
pic.LineColor = Color.DarkRed;
pic.LineWidth = 100;
// add the pic to specified sheet's ShapeCollection
XLSheet sheet = wb.Sheets("Forecasting Report");
sheet.Shapes.Add(pic);
Save and open the book.
wb.Save("C:\Project\WorkBook1.xls ")
System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
wb.Save(@"C:\Project\WorkBook1.xls");
System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
In this example, the shape was added to the sheet's ShapeCollection; therefore, the image does not replace the value in the first cell. Here we specified the height and width of the image, as well as the horizontal and vertical positioning.