Search code examples
c#bitmapexcel-2007epplusepplus-4

Why is my bitmap appearing off-centered (one cell too far right and down)?


I have this EPPlus code to place a bitmap on an Excel spreadsheet at column 5/E, row 1:

using (var package = new ExcelPackage(file))
{
    . . .
    AddImage(locationWorksheet, 1, 5, imgPath);
    . . .
}

private void AddImage(ExcelWorksheet oSheet, int rowIndex, int colIndex, string imagePath)
{
    Bitmap image = new Bitmap(imagePath);
    {
        var excelImage = oSheet.Drawings.AddPicture("PRO*ACT Logo", image);
        excelImage.From.Column = colIndex;
        excelImage.From.Row = rowIndex;
        excelImage.SetSize(108, 84);
    }
}

The image is a .png file, with the dimensions shown (108X84).

Although the image should display at tghe intersection of row 1 and column 5, it's actually more like row 2 and column 6:

enter image description here

Why, and how can I rectify this?

Note: Excel column and row indexes are 1-based (not 0).


Solution

  • Perhaps AddImage is looking for the index of the row/column rather than the row/column number. If that is the case, then you'd count the rows/columns as 0, 1, 2, 3, 4, etc.

    If you subtract one from your current values you should be ok. It is relatively common to use an index to reference a location in a list or a grid, so I'd say that's a safe bet.