|
| 1 | +--- |
| 2 | +title: Align a picture inside a cell in an Excel worksheet | Syncfusion |
| 3 | +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. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to align a picture inside a cell in an Excel worksheet? |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + int row = 2; |
| 23 | + int column = 3; |
| 24 | + |
| 25 | + //Adding a picture |
| 26 | + FileStream imageStream = new FileStream("../../../Data/Image.png", FileMode.Open, FileAccess.Read); |
| 27 | + IPictureShape shape = worksheet.Pictures.AddPicture(row, column, imageStream); |
| 28 | + |
| 29 | + //Insert the image into the cell |
| 30 | + (shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row); |
| 31 | + (shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column); |
| 32 | + |
| 33 | + //Algin the image inside the cell |
| 34 | + (shape as ShapeImpl).TopRowOffset = 50; |
| 35 | + (shape as ShapeImpl).LeftColumnOffset = 50; |
| 36 | + |
| 37 | + #region Save |
| 38 | + //Saving the workbook |
| 39 | + workbook.SaveAs("../../../Output/Picture.xlsx"); |
| 40 | + #endregion |
| 41 | + |
| 42 | + //Dispose streams |
| 43 | + imageStream.Dispose(); |
| 44 | +} |
| 45 | +{% endhighlight %} |
| 46 | + |
| 47 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 48 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 49 | +{ |
| 50 | + IApplication application = excelEngine.Excel; |
| 51 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 52 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 53 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 54 | + |
| 55 | + int row = 2; |
| 56 | + int column = 3; |
| 57 | + |
| 58 | + //Adding a picture |
| 59 | + string image = "../../Data/Image.png"; |
| 60 | + IPictureShape shape = worksheet.Pictures.AddPicture(row, column, image); |
| 61 | + |
| 62 | + // Insert the image into the cell |
| 63 | + (shape as ShapeImpl).Height = worksheet.GetRowHeightInPixels(row); |
| 64 | + (shape as ShapeImpl).Width = worksheet.GetColumnWidthInPixels(column); |
| 65 | + |
| 66 | + //Algin the image inside the cell |
| 67 | + (shape as ShapeImpl).TopRowOffset = 50; |
| 68 | + (shape as ShapeImpl).LeftColumnOffset = 50; |
| 69 | + |
| 70 | + #region Save |
| 71 | + //Saving the workbook |
| 72 | + workbook.SaveAs("../../Output/Picture.xlsx"); |
| 73 | + #endregion |
| 74 | +} |
| 75 | +{% endhighlight %} |
| 76 | + |
| 77 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 78 | + Using excelEngine As New ExcelEngine() |
| 79 | + Dim application As IApplication = excelEngine.Excel |
| 80 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 81 | + |
| 82 | + Dim workbook As IWorkbook = application.Workbooks.Create(1) |
| 83 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 84 | + |
| 85 | + Dim row As Integer = 2 |
| 86 | + Dim column As Integer = 3 |
| 87 | + |
| 88 | + ' Adding a picture |
| 89 | + Dim image As String = "../../Data/Image.png" |
| 90 | + Dim shape As IPictureShape = worksheet.Pictures.AddPicture(row, column, image) |
| 91 | + |
| 92 | + ' Insert the image into the cell |
| 93 | + Dim impl As ShapeImpl = CType(shape, ShapeImpl) |
| 94 | + impl.Height = worksheet.GetRowHeightInPixels(row) |
| 95 | + impl.Width = worksheet.GetColumnWidthInPixels(column) |
| 96 | + |
| 97 | + ' Align the image inside the cell |
| 98 | + impl.TopRowOffset = 50 |
| 99 | + impl.LeftColumnOffset = 50 |
| 100 | + |
| 101 | + ' Save |
| 102 | + workbook.SaveAs("../../Output/Picture.xlsx") |
| 103 | + End Using |
| 104 | +{% endhighlight %} |
| 105 | +{% endtabs %} |
0 commit comments