Frage

I want to print the entire form of size (1415x1000). But it is printing the form of size (1185x740).

I refer to the code from the msdn website:

 [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private 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);
        IntPtr dc1 = mygraphics.GetHdc();

        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369377);//13369376);

        mygraphics.ReleaseHdc(dc1);

        memoryGraphics.ReleaseHdc(dc2);

    }

  private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

 private void button1_Click_1(object sender, EventArgs e)
    {
        panel1.Hide();
        CaptureScreen();
        PrintDocument myDoc = new PrintDocument();
        myDoc.PrinterSettings.DefaultPageSettings.PaperSize = new    System.Drawing.Printing.PaperSize("Custom", 1415, 1000);
        PrintPreviewDialog print_dlg = new PrintPreviewDialog();
        print_dlg.Document = printDocument1;
        print_dlg.ShowDialog();
    }

But I cant get the complete form in the printout. How do I do that?

War es hilfreich?

Lösung

The default scaling used by the Graphics object created by PrintDocument is GraphicsUnit.Display. Which maps 100 pixels to an inch. That's usually a good match with the video adapter's dots-per-inch setting, the default is 96 pixels per inch. So whatever you print on paper is roughly the same size as it was on your monitor.

That doesn't work so well for your screenshot however, your monitor is too big. Or the paper you use is too small, take your pick ;) You will have to draw it smaller. Either use the Graphics.DrawImage(Image, Rectangle) overload or, simpler, use Graphics.ScaleTransform() to draw it smaller. You probably also want to use Graphics.TranslateTransform() so the image is centered on paper. Use e.MarginBounds to find the size of the piece of paper. Some sample code for the PrintDocument event handlers that demonstrate how to make this work for any monitor size:

    private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e) {
        e.PageSettings.Landscape = true;
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        // Make screenshot
        var scr = Screen.FromPoint(this.Location);
        using (var bmp = new Bitmap(scr.Bounds.Width, scr.Bounds.Height)) {
            using (var gr = Graphics.FromImage(bmp)) {
                gr.CopyFromScreen(new Point(scr.Bounds.Left, scr.Bounds.Top), Point.Empty, bmp.Size);
            }
            // Determine scaling
            float scale = 1.0f;
            scale = Math.Min(scale, (float)e.MarginBounds.Width / bmp.Width);
            scale = Math.Min(scale, (float)e.MarginBounds.Height / bmp.Height);
            // Set scaling and offset
            e.Graphics.TranslateTransform(e.MarginBounds.Left + (e.MarginBounds.Width - bmp.Width * scale) / 2, 
                                          e.MarginBounds.Top + (e.MarginBounds.Height - bmp.Height * scale) / 2);
            e.Graphics.ScaleTransform(scale, scale);
            // And draw
            e.Graphics.DrawImage(bmp, 0, 0);
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top