C# 为什么粘贴到Excel时图像显示的大小大于其实际大小,如何使其以自然大小显示?

C# 为什么粘贴到Excel时图像显示的大小大于其实际大小,如何使其以自然大小显示?,c#,image,excel-interop,image-scaling,image-size,C#,Image,Excel Interop,Image Scaling,Image Size,我有以下代码将图像粘贴到Excel中: . . . string unitImageLoc = GetUnitImageLoc(); if (unitImageLoc != "image not found") { Image img = Image.FromFile(unitImageLoc); int imgWidth = img.Width; int imgHeight = img.Height; _xlSheet.Shapes.AddPicture(uni

我有以下代码将图像粘贴到Excel中:

. . .
string unitImageLoc = GetUnitImageLoc();
if (unitImageLoc != "image not found")
{
    Image img = Image.FromFile(unitImageLoc);
    int imgWidth = img.Width;
    int imgHeight = img.Height;
    _xlSheet.Shapes.AddPicture(unitImageLoc, 
Microsoft.Office.Core.MsoTriState.msoFalse, 
Microsoft.Office.Core.MsoTriState.msoCTrue, 4, 4, imgWidth, imgHeight); 
}

private string GetUnitImageLoc()
{
    string unitUpper = _unit.ToUpper();
    string candidateFile = string.Format("C:\\RoboReporter\\{0}.png", 
unitUpper);
    if (File.Exists(candidateFile))
    {
        return candidateFile;
    }
    return "image not found";
}
它可以工作,但打印的图像比实际尺寸大,如图所示(顶部为Excel,底部为image viewer):

这不是特定图像的问题:它发生在任何:


因此,我需要将宽度和高度乘以70%或是其他什么东西才能使其相同吗?

尝试输入
-1
以获得所述的
宽度和
高度。

可能尝试输入
-1
以获得所述的宽度和高度。如果您自己复制并粘贴图像,会发生什么?如前所述,Excel基于物理大小显示图像,因此它使用图像的DPI设置来缩放图像;它不只是将一个图像像素映射到一个屏幕像素,这仅在图像DPI与Windows设置相同时发生。您可能需要将图像读入图像类,更改其垂直和水平分辨率属性,然后将其重新保存到临时文件并将该临时文件添加到电子表格中。您可以非常轻松地设置图像的dpi@TaW:但是知道如何让事情变得容易吗?还是仅仅是一个试验和例外的问题?@Quantic:如果你能用“-1”作为答案,我会这样做;它工作正常,使前两个参数为0,后两个参数为-1。