Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -5879,6 +5879,18 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/what-is-the-unit-of-row-height-and-column-width-in-Excel">What is the unit of row height and column width in Excel?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-apply-the-formatting-for-a-particular-column-while-importing-data-from-collection-objects">How to apply formatting to a specific column while importing data?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-align-the-image-inside-the-cell">How to align a picture inside a cell in an Excel worksheet?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-resolve-the-cannot-open-Pivot-table-source-file-error">How to resolve the cannot open pivot table source file error?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-extract-embedded-OLE-files-from-an-Excel-workbook-as-streams">How to extract embedded OLE files from an Excel workbook as streams?</a>
</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: Align a picture inside a cell in an Excel worksheet | Syncfusion
description: Learn how to align an image precisely within a worksheet cell using the Syncfusion .NET Excel (XlsIO) library, including positioning, fitting to the cell.
platform: document-processing
control: XlsIO
documentation: UG
---

# How to align a picture inside a cell in an Excel worksheet?

Image can be aligned in the cell as required using the **TopRowOffset** and **LeftColumnOffset** properties of **ShapeImpl** instance. The following example shows how to align a picture inside a worksheet cell using the Syncfusion .NET Excel (XlsIO) library.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

int row = 2;
int column = 3;

//Adding a picture
FileStream imageStream = new FileStream("../../../Data/Image.png", FileMode.Open, FileAccess.Read);
IPictureShape shape = worksheet.Pictures.AddPicture(row, column, imageStream);

//Insert the image into the cell
(shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row);
(shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column);

//Algin the image inside the cell
(shape as ShapeImpl).TopRowOffset = 50;
(shape as ShapeImpl).LeftColumnOffset = 50;

#region Save
//Saving the workbook
workbook.SaveAs("../../../Output/Picture.xlsx");
#endregion

//Dispose streams
imageStream.Dispose();
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

int row = 2;
int column = 3;

//Adding a picture
string image = "../../Data/Image.png";
IPictureShape shape = worksheet.Pictures.AddPicture(row, column, image);

// Insert the image into the cell
(shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row);
(shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column);

//Algin the image inside the cell
(shape as ShapeImpl).TopRowOffset = 50;
(shape as ShapeImpl).LeftColumnOffset = 50;

#region Save
//Saving the workbook
workbook.SaveAs("../../Output/Picture.xlsx");
#endregion
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx

Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)

Dim row As Integer = 2
Dim column As Integer = 3

' Adding a picture
Dim image As String = "../../Data/Image.png"
Dim shape As IPictureShape = worksheet.Pictures.AddPicture(row, column, image)

' Insert the image into the cell
Dim impl As ShapeImpl = CType(shape, ShapeImpl)
impl.Height = worksheet.GetRowHeightInPixels(row)
impl.Width = worksheet.GetColumnWidthInPixels(column)

' Align the image inside the cell
impl.TopRowOffset = 50
impl.LeftColumnOffset = 50

' Save
workbook.SaveAs("../../Output/Picture.xlsx")
End Using
{% endhighlight %}
{% endtabs %}