由于列宽扩大,EPPlus徽标图像被拉伸

时间:2019-04-12 07:33:47

标签: c# epplus

Epplus,excel图纸,徽标首先在第2列绘制,然后在其下方绘制相应的表格,但是由于表格的列宽是可变的,因此它将自动展开徽标图像

这就是我想要的样子 RequiredImagelink

但这就是渲染方式 ActualImageLink

图像渲染代码

double imageColumnStartOffset = 0;
for (var i = 1; i < position.CurrentColumn; i++)
{
    imageColumnStartOffset += excelWorkSheet.Column(i).Width;
}

int rowKey = Thread.CurrentThread.ManagedThreadId; //Using the current thread id as the unique key. Any better ideas? If yes, feel free to change this :)
if (!ModifiedRowCollection.ContainsKey(rowKey))
{
    excelWorkSheet.Row(position.CurrentRow).Height = Pixel2Points(height);
    ModifiedRowCollection.Add(rowKey, position.CurrentRow);
}


image.SetPosition(Points2PixelRow((int)imageRowStartOffset), Points2PixelColumn((int)imageColumnStartOffset));
image.SetSize(width, height);

表格图像绘图代码

if (cell.CellDataFormat == CellDataFormat.Image)
{
    Image image = null;

        if (((Image)cell.Value) != null)
        {
            //Its a stream
            image = (Image)cell.Value;
        }
        else
            image = null;



    if (image != null)
    {
        //excelWorkSheet.Drawings.AddPicture()

        var pic = excelWorkSheet.Drawings.AddPicture("Pic" + Guid.NewGuid(), image);

        pic.From.Column = pic.To.Column = position.CurrentColumn - 1;
        pic.From.Row = pic.To.Row = position.CurrentRow - 1;

        pic.SetPosition(pic.From.Row, 5, pic.From.Column, 5);

        excelWorkSheet.Row(position.CurrentRow).Height = pic.Image.Height;

        imageCollection.Add(new Dictionary<int, ExcelPicture>() { { position.CurrentColumn, pic } });


    }
}
if (i == (totalRows - 1)) 
  {     

      foreach (var image in imageCollection)
      {
          var column = image.Keys.FirstOrDefault();
          var pic = image[column];
          excelWorkSheet.Column(column).Width = 300/(7.5) +5;                           
          pic.SetSize(pic.Image.Width, pic.Image.Height);

      }    


      TableWidth += excelWorkSheet.Column(position.CurrentColumn).Width;                        
  }

我尝试过的 1.在绘制图像之前固定列大小。 2.锁定行 3.徽标的实际大小为188 * 56,我尝试将其减小为100 * 56,然后就没有发生拉伸,我的假设是,只有在图像大小大于默认列大小时才发生拉伸。

0 个答案:

没有答案