EPPlus detect image aspect ratio
Asked Answered
G

1

0

I am working with EPPlus and try to place a image into B1 cell. I found a nice post which show how to detect aspect ratio and set size for image accordingly.

I saw his code but still few things are not clear. here is code taken from https://mcmap.net/q/454401/-epplus-setposition-picture-issue

public static void CreatePicture(ExcelWorksheet worksheet, string name, Image image, int firstColumn, int lastColumn, int firstRow, int lastRow, int defaultOffsetPixels)
{
    int columnWidth = GetWidthInPixels(worksheet.Cells[firstRow, firstColumn]);
    int rowHeight = GetHeightInPixels(worksheet.Cells[firstRow, firstColumn]);

    int totalColumnWidth = columnWidth * (lastColumn - firstColumn + 1);
    int totalRowHeight = rowHeight * (lastRow - firstRow + 1);
    double cellAspectRatio = Convert.ToDouble(totalColumnWidth) / Convert.ToDouble(totalRowHeight);

    int imageWidth = image.Width;
    int imageHeight = image.Height;
    double imageAspectRatio = Convert.ToDouble(imageWidth) / Convert.ToDouble(imageHeight);

    int pixelWidth;
    int pixelHeight;
    if (imageAspectRatio > cellAspectRatio)
    {
        pixelWidth = totalColumnWidth - defaultOffsetPixels * 2;
        pixelHeight = pixelWidth * imageHeight / imageWidth;
    }
    else
    {
        pixelHeight = totalRowHeight - defaultOffsetPixels * 2;
        pixelWidth = pixelHeight * imageWidth / imageHeight;
    }

    int rowOffsetPixels = (totalRowHeight - pixelHeight) / 2;
    int columnOffsetPixels = (totalColumnWidth - pixelWidth) / 2;

    int rowOffsetCount = 0;
    int columnOffsetCount = 0;

    if (rowOffsetPixels > rowHeight)
    {
        rowOffsetCount = (int)Math.Floor(Convert.ToDouble(rowOffsetPixels) / Convert.ToDouble(rowHeight));
        rowOffsetPixels -= rowHeight * rowOffsetCount;
    }

    if (columnOffsetPixels > columnWidth)
    {
        columnOffsetCount = (int)Math.Floor(Convert.ToDouble(columnOffsetPixels) / Convert.ToDouble(columnWidth));
        columnOffsetPixels -= columnWidth * columnOffsetCount;
    }

    int row = firstRow + rowOffsetCount - 1;
    int column = firstColumn + columnOffsetCount - 1;

    ExcelPicture pic = worksheet.Drawings.AddPicture(name, image);
    pic.SetPosition(row, rowOffsetPixels, column, columnOffsetPixels);
    pic.SetSize(pixelWidth, pixelHeight);
}

public static int GetHeightInPixels(ExcelRange cell)
{
    using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
    {
        float dpiY = graphics.DpiY;
        return (int)(cell.Worksheet.Row(cell.Start.Row).Height * (1 / 72.0) * dpiY);
    }
}

public static float MeasureString(string s, Font font)
{
    using (var g = Graphics.FromHwnd(IntPtr.Zero))
    {
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        return g.MeasureString(s, font, int.MaxValue, StringFormat.GenericTypographic).Width;
    }
}

public static int GetWidthInPixels(ExcelRange cell)
{
    double columnWidth = cell.Worksheet.Column(cell.Start.Column).Width;
    Font font = new Font(cell.Style.Font.Name, cell.Style.Font.Size, FontStyle.Regular);
    double pxBaseline = Math.Round(MeasureString("1234567890", font) / 10);
    return (int)(columnWidth * pxBaseline);
}

int firstColumn, int lastColumn, int firstRow, int lastRow, int defaultOffsetPixels

  1. I like to know what will be the value for firstColumn
  2. I like to know what will be the value for lastColumn
  3. I like to know what will be the value for firstRow
  4. I like to know what will be the value for lastRow
  5. I like to know what will be the value for defaultOffsetPixels

Anyone can guide me how to calculate values for above points.

Regarding the points 1 to 4 can i use the below code to get first, last row and first column and last column ?

var start = workSheet.Dimension.Start;  
var end = workSheet.Dimension.End;  

int startrow = start.Row;
int endrow = end.Row;

int startcol = start.Column
int endcol = end.Column

OR should i use these code to get first, last row and first and last column ? please suggest.

int numRow = worksheet.Dimension.Rows;
int numCol = worksheet.Dimension.Columns;
string lastAddress = worksheet.Dimension.Address.Last().ToString();

How to calculate defaultOffsetPixels ?

Thanks

Georginageorgine answered 11/9, 2021 at 9:49 Comment(1)
#21742538 codereview.stackexchange.com/questions/139569/…Georginageorgine
A
1

Well,firstColumn,lastColumn,firstRow,lastRow,defaultOffsetPixels is incoming Parameter。I have an example of how it works,I hope it helps

Image image = Image.FromStream(new MemoryStream(drs[0]["Image"] as byte[]));
                        EPPlusPicture.CreatePicture(worksheet, Guid.NewGuid().ToString(), image, col, col, firstRow, lastRow, 5);
Aquamarine answered 29/11, 2021 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.