Home > Back-end >  Fit PrintDocument to an A4
Fit PrintDocument to an A4

Time:05-17

I wrote(copied) a small code to select some image files and print it every five minutes.

When it prints, the image doesn't fit to the paper. It's smaller or bigger. So I want to fit the Images to an A4 Page Size. I couldn't find any properties for that.

Is there a way to do that?

Here is my code:

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");
            }
        }

CodePudding user response:

You do not need to define Point;
To fit image to a page just use args.Graphics.DrawImage(i, args.PageBounds); instead of args.Graphics.DrawImage(i, p);

  • Related