This project has moved and is read-only. For the latest updates, please go here.

Align picture in middle of cell (ASP.Net/C#)

Nov 25, 2013 at 4:05 PM
Hi guys,

I hope you are doing great.

I am working with EPPlus library to generate excel files in ASP.Net/C#, and I want to insert an image in each row, but image should be aligned in middle of the cell (or may be on top but with some margin). Below is the code which I am using.
System.Drawing.Image img = System.Drawing.Image.FromFile(MapPath("~/images/myimage.jpg"));
ExcelPicture pic = ws.Drawings.AddPicture("My Image", img);
int left = 0;
int top = 0;
CalculateCoodinates(row, 2, ws, out top, out left);
pic.SetSize(80, 50);
pic.SetPosition(top, left);

// Method to calculate top and left coordinates
void CalculateCoodinates(int row, int column, ExcelWorksheet worksheet, out int top, out int left)
    double t = 5; // 5 - to add some extra padding
    left = 66;

    for (int i = 1; i < row; i++)
        t += (int)worksheet.Row(i).Height;

    top = (int)Math.Ceiling(t / 0.75);
I tried different ways, but I didn't get the images in proper cell. In top few cells, images are appearing fine, but after that images are moving down.

I also tried:
pic.From.Column = 1;
pic.From.Row = (row - 1);
but it's aligning image on top border of the cell which I don't want.

Any help will be highly appreciated.

Many thanks...