使 PrintDocument 适合 A4

Fit PrintDocument to an A4

我写(复制)了一个小代码到select一些图像文件并每五分钟打印一次。

打印时,图像不适合纸张。它更小或更大。所以我想将图像调整为 A4 页面大小。 我找不到任何属性。

有办法吗?

这是我的代码:

private async void button1_Click(object btnSender, EventArgs e)
        {
            // Set the file dialog to filter for graphics files.
            this.openFileDialog1.Filter =
                "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
                "All files (*.*)|*.*";

            // Allow the user to select multiple images.
            this.openFileDialog1.Multiselect = true;
            this.openFileDialog1.Title = "My Image Browser";
            DialogResult dr = this.openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                // Read the files
                foreach (string file in openFileDialog1.FileNames)
                {
                    PrintDialog printDlg = new PrintDialog();
                    PrintDocument printDoc = new PrintDocument();
                    printDoc.DocumentName = "Print Document";
                    printDlg.Document = printDoc;
                    printDlg.AllowSelection = true;
                    printDlg.AllowSomePages = true;

                    printDoc.DefaultPageSettings.Landscape = true;

                    printDoc.PrintPage += (sender, args) =>
                    {
                        Image i = Image.FromFile(file);
                        Point p = new Point(0, 0);
                        args.Graphics.DrawImage(i, p);
                    };

                    printDoc.Print();

                    await Task.Delay(300000);
                }

                Process.Start("shutdown.exe", "-s -t 00");
            }
        }

不需要定义Point;
要使图像适合页面,只需使用 args.Graphics.DrawImage(i, args.PageBounds); 而不是 args.Graphics.DrawImage(i, p);