【问题标题】:EPPlus detect image aspect ratioEPPlus 检测图像纵横比
【发布时间】:2021-11-29 11:09:38
【问题描述】:

我正在使用 EPPlus 并尝试将图像放入 B1 单元格。我找到了一篇不错的帖子,它展示了如何检测纵横比并相应地设置图像大小。

我看到了他的代码,但仍有几件事不清楚。这是取自https://stackoverflow.com/a/54941008/15940620的代码

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. 我想知道 firstColumn 的值是多少
  2. 我想知道 lastColumn 的值是多少
  3. 我想知道 firstRow 的值是多少
  4. 我想知道 lastRow 的值是多少
  5. 我想知道 defaultOffsetPixels 的值是多少

任何人都可以指导我如何计算上述点的值。

关于点 1 到 4,我可以使用下面的代码来获取第一、最后一行、第一列和最后一列吗?

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

或者我应该使用这些代码来获取第一行、最后一行以及第一列和最后一列吗?请提出建议。

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

如何计算defaultOffsetPixels

谢谢

【问题讨论】:

标签: epplus aspect-ratio


【解决方案1】:

嗯,firstColumn,lastColumn,firstRow,lastRow,defaultOffsetPixels是传入参数。我有一个例子说明它是如何工作的,希望对你有帮助

Image image = Image.FromStream(new MemoryStream(drs[0]["Image"] as byte[]));
                        EPPlusPicture.CreatePicture(worksheet, Guid.NewGuid().ToString(), image, col, col, firstRow, lastRow, 5);

【讨论】:

    猜你喜欢
    • 2016-01-18
    • 2018-10-25
    • 2019-04-02
    • 2013-02-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多