docs: Update readme

This commit is contained in:
Josh Hendricks
2022-02-02 08:16:22 -08:00
parent 91da711635
commit cf5d3f83d6
2 changed files with 21 additions and 7 deletions

View File

@@ -25,10 +25,10 @@ try {
$xlpkg = $data | Export-Excel -Path $path -PassThru
$xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell
} finally {
$image.Dispose()
if ($image) {
$image.Dispose()
}
if ($xlpkg) {
Close-ExcelPackage -ExcelPackage $xlpkg -Show
}
}

View File

@@ -6,14 +6,28 @@ method on the `Drawings` property of an `ExcelWorksheet` object.
The `Add-ExcelImage` example here demonstrates how to add a picture at a given
cell location, and optionally resize the row and column to fit the image.
## Running the example
To try this example, run the script `AddPicture.ps1`. The `Add-ExcelImage`
function will be dot-sourced, and an Excel document will be created in the same
folder with a sample data set. The Octocat image will then be embedded into
Sheet1.
The creation of the Excel document and the `System.Drawing.Image` object
representing Octocat are properly disposed within a `finally` block to ensure
that the resources are released, even if an error occurs in the `try` block.
## Note about column and row sizing
Care has been taken in this example to get the image placement to be just inside
the cell border, and if the `-ResizeCell` switch is present, the height and width
of the row and column will be increased, if needed, so that the bottom right of
the image also lands just inside the cell border.
The Excel row and column sizes are measured in "point" units rather than pixels,
and at the moment a fixed multiplication factor is used to convert the size of
the image in pixels, to the corresponding height and width values in Excel.
and a fixed multiplication factor is used to convert the size of the image in
pixels, to the corresponding height and width values in Excel.
You may find that images with a different DPI, or different resolution, DPI, or
text scaling options result in imperfect row and column sizing.
It's possible that different DPI or text scaling options could result in
imperfect column and row sizing and if a better strategy is found for converting
the image dimensions to column and row sizes, this example will be updated.