简体   繁体   English

什么决定着直流缩放?

[英]What governs DC scaling?

This code gets different scaling depending on which computer I run it on. 根据我在哪台计算机上运行,​​此代码将获得不同的缩放比例。

        Metafile image;
        IntPtr dib;
        var memoryHdc = Win32Utils.CreateMemoryHdc(IntPtr.Zero, 1, 1, out dib);
        try
        {
            image = new Metafile(memoryHdc, EmfType.EmfOnly);

            using (var g = Graphics.FromImage(image))
            {
                Render(g, html, left, top, maxWidth, cssData, stylesheetLoad, imageLoad);
            }
        }
        finally
        {
            Win32Utils.ReleaseMemoryHdc(memoryHdc, dib);
        }

Going into the Render method, the Metafile object has a PixelFormat of DontCare and consequently does not have valid vertical or horizontal resolutions. 进入Render方法时,图元文件对象的像素格式为DontCare,因此没有有效的垂直或水平分辨率。

Coming out of the Render method, it has a value of Format32bppRgb and PhysicalDimension.Width and PhysicalDimension.Height have increased to accommodate the rendered image. 来自Render方法,其值为Format32bppRgb ,并且增加了PhysicalDimension.WidthPhysicalDimension.Height来容纳渲染的图像。

How can I make scaling independent of local settings? 如何使缩放比例独立于本地设置?

Here's the implementation of CreateMemoryHdc (I didn't write it, it's from an OSS library). 这是CreateMemoryHdc的实现(我没有写,它来自OSS库)。

    public static IntPtr CreateMemoryHdc(IntPtr hdc, int width, int height, out IntPtr dib)
    {
        // Create a memory DC so we can work off-screen
        IntPtr memoryHdc = CreateCompatibleDC(hdc);
        SetBkMode(memoryHdc, 1);

        // Create a device-independent bitmap and select it into our DC
        var info = new BitMapInfo();
        info.biSize = Marshal.SizeOf(info);
        info.biWidth = width;
        info.biHeight = -height;
        info.biPlanes = 1;
        info.biBitCount = 32;
        info.biCompression = 0; // BI_RGB
        IntPtr ppvBits;
        dib = CreateDIBSection(hdc, ref info, 0, out ppvBits, IntPtr.Zero, 0);
        SelectObject(memoryHdc, dib);

        return memoryHdc;
    }

As you can see, the width, height and bit depth passed to the DC constructor are constant. 如您所见,传递给DC构造函数的宽度,高度和位深度是恒定的。 Creating the metafile produces different physical dimensions. 创建图元文件会产生不同的物理尺寸。 Right after executing this 执行完之后

            image = new Metafile(memoryHdc, EmfType.EmfOnly);

the metafile has PhysicalDimension.Height (and width) of 26.43 on my workstation and 31.25 on the server to which I am deploying, so the difference in scaling is already evident and therefore probably not a consequence of anything in the rendering. 图元文件的PhysicalDimension.Height (和宽度)在我的工作站上为26.43,在我要部署到的服务器上为31.25,因此缩放的差异已经很明显,因此可能不是渲染中的任何结果。

This may be relevant. 这可能是相关的。 BitMapInfo is defined in the OSS library and looks like this: BitMapInfo在OSS库中定义,如下所示:

internal struct BitMapInfo
{
    public int biSize;
    public int biWidth;
    public int biHeight;
    public short biPlanes;
    public short biBitCount;
    public int biCompression;
    public int biSizeImage;
    public int biXPelsPerMeter;
    public int biYPelsPerMeter;
    public int biClrUsed;
    public int biClrImportant;
    public byte bmiColors_rgbBlue;
    public byte bmiColors_rgbGreen;
    public byte bmiColors_rgbRed;
    public byte bmiColors_rgbReserved;
}

so possibly setting biXPelsPerMeter and biYPelsPerMeter will help. 因此设置biXPelsPerMeterbiYPelsPerMeter可能会有所帮助。 The above code doesn't set them and may be allowing platform values. 上面的代码设置它们,可能允许使用平台值。

Unfortunately, setting these values doesn't seem to make any difference. 不幸的是,设置这些值似乎没有任何区别。 msdn says msdn说

biXPelsPerMeter biXPelsPerMeter

The horizontal resolution, in pixels-per-meter, of the target device for the bitmap. 位图目标设备的水平分辨率(以每米像素为单位)。 An application can use this value to select a bitmap from a resource group that best matches the characteristics of the current device. 应用程序可以使用此值从资源组中选择最匹配当前设备特征的位图。

So these settings are used when loading a bitmap from a resource . 因此, 从资源 加载位图时使用这些设置。 No help here. 这里没有帮助。

This all looks pertinent https://www.codeproject.com/articles/177394/%2fArticles%2f177394%2fWorking-with-Metafile-Images-in-NET 这一切看起来都很相关https://www.codeproject.com/articles/177394/%2fArticles%2f177394%2fWorking-with-Metafile-Images-in-NET

It may help to know that this code does not run in an application. 知道此代码未在应用程序中运行可能会有所帮助。 It renders HTML as a metafile for printing, and it lives inside a Web API webservice. 它将HTML呈现为用于打印的图元文件,并且位于Web API网络服务中。

There is no user interface so I'm not sure how to interpret the question of whether it is DPI Aware. 没有用户界面,所以我不确定如何解释它是否为DPI Aware问题。 The evidence suggests it's DPI affected so the question may be pertinent. 有证据表明它受到 DPI的影响,因此该问题可能是相关的。

GDI doesn't scale. GDI无法扩展。 Use GDI+ for device independence. 使用GDI +来实现设备独立性。 You will lose antialiasing but most print devices are high DPI anyway. 您将失去抗锯齿功能,但是大多数打印设备的DPI仍然很高。

Does the library in use have an option to use GDI+ instead? 使用中的库是否可以选择使用GDI +?

(In my own case, yes. Problem solved.) (就我自己而言,是的。问题已解决。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM