PixelToNewDpi

ConvertUtil.PixelToNewDpi method

将像素从一种分辨率转换为另一种分辨率。

public static int PixelToNewDpi(double pixels, double oldDpi, double newDpi)
范围类型描述
pixelsDouble要转换的值。
oldDpiDouble当前 dpi(每英寸点数)分辨率。
newDpiDouble新的 dpi(每英寸点数)分辨率。

例子

演示如何使用默认和自定义分辨率将点转换为像素。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 根据自定义 DPI 定义此部分上边距的大小(以像素为单位)。
const double myDpi = 192;

PageSetup pageSetup = builder.PageSetup;
pageSetup.TopMargin = ConvertUtil.PixelToPoint(100, myDpi);

Assert.AreEqual(37.5d, pageSetup.TopMargin, 0.01d);

// 默认 DPI 为 96 时,一个像素为 0.75 点。
Assert.AreEqual(0.75d, ConvertUtil.PixelToPoint(1));

builder.Writeln($"This Text is {pageSetup.TopMargin} points/{ConvertUtil.PointToPixel(pageSetup.TopMargin, myDpi)} " +
                $"pixels (at a DPI of {myDpi}) from the top of the page.");

// 设置新的 DPI 并相应调整上边距值。
const double newDpi = 300;
pageSetup.TopMargin = ConvertUtil.PixelToNewDpi(pageSetup.TopMargin, myDpi, newDpi);
Assert.AreEqual(59.0d, pageSetup.TopMargin, 0.01d);

builder.Writeln($"At a DPI of {newDpi}, the text is now {pageSetup.TopMargin} points/{ConvertUtil.PointToPixel(pageSetup.TopMargin, myDpi)} " +
                "pixels from the top of the page.");

doc.Save(ArtifactsDir + "UtilityClasses.PointsAndPixelsDpi.docx");

也可以看看