EPPlus检测图像长宽比



我正在使用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

我想知道firstColumn的值是多少我想知道lastColumn的值是多少我想知道firstRow的值是什么
  • 我想知道lastRow的值是什么
  • 我想知道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

    ?

    感谢

    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);

    相关内容

    • 没有找到相关文章

    最新更新