【问题标题】:My form is not printing correctly when DPI is 150%当 DPI 为 150% 时,我的表单无法正确打印
【发布时间】:2016-03-29 20:47:36
【问题描述】:

我有一个可以在我的机器上正确打印的表单,但是当我在另一台机器上部署应用程序时,该表单不适合页面并且桌面背景出现在打印的文档上。两台机器的主要区别在于一台机器的 DPI 设置为 150%。我已经多次更改自动缩放,但没有任何变化。表格在屏幕上看起来不错,但打印不正确。下面是我正在使用的代码。

private void btnPrint_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();            
    }

    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,
           System.Drawing.Printing.PrintPageEventArgs e)
    {            
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

【问题讨论】:

  • 您为什么要打印网络表单?你确定区别不在于两台机器的打印设置?
  • 用户需要填写、保存和打印Windows表单。当 DPI 为 100% 或 125% 但不是 150% 时,表格打印正确。我的一位用户有视力问题,因此他使用最高 DPI 设置。
  • 您有一张失败打印输出的照片吗?
  • 我会试着弄一个。描述它的最佳方式是,如果您从左上角开始,然后向右和向下移动大约 50% 的表单大小,放大然后裁剪表单,这就是您所得到的。

标签: c# winforms scale


【解决方案1】:

更高的 dpi 缩放(如旧的 125% 缩放)不是通过增加 Windows 字体大小并让应用程序处理缩放来实现的,而是让操作系统为您进行缩放。在这种模式下,操作系统会向应用程序谎报实际的 dpi 设置,并在绘制其表面时自行缩放应用程序。

结果是在您的应用程序内部,像素位置和大小不是屏幕上使用的真实值。但是CopyFromScreen() 方法需要实际的像素坐标和大小。您需要找出您的应用程序经历的像素缩放,然后将此缩放应用于您使用的坐标。

这是工作代码(getScalingFactor() 方法是从this answer 窃取的)。

[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
    VERTRES = 10,
    DESKTOPVERTRES = 117,
}

private float getScalingFactor()
{
    using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
    {
        IntPtr desktop = g.GetHdc();
        try
        {
            int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
            int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
            float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;
            return ScreenScalingFactor; 
        }
        finally
        {
            g.ReleaseHdc();
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    using (Graphics myGraphics = this.CreateGraphics())
    {
        var factor = getScalingFactor();
        Size s = new Size((int)(this.Size.Width * factor), (int)(this.Size.Height * factor));

        using (Bitmap memoryImage = new Bitmap(s.Width, s.Height, myGraphics))
        {
            using (Graphics memoryGraphics = Graphics.FromImage(memoryImage))
            {
                memoryGraphics.CopyFromScreen((int)(Location.X * factor), (int)(Location.Y * factor), 0, 0, s);
                memoryImage.Save(@"D:\x.png", ImageFormat.Png);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 2020-04-24
    • 2022-12-10
    • 2023-03-26
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多