日期:2014-05-17  浏览次数:21195 次

c#打印预览窗体结果是一片空白
想实现打印窗体和窗体上的控件,参考了大家的代码,结果点击打印的时候,预览的结果是一边显示完了整个窗体,但是旁边多出了一块空白,请教各位这应该怎么解决呢。谢谢啊。



C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ContractFill
{
    public partial class ZWebSite : Form
    {
        public ZWebSite(ContractFill.Main parent)
        {
            InitializeComponent();
            this.MdiParent = parent;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        [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, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printPreviewDialog1.ShowDialog();
        }
 
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

    }
}





------解决方案--------------------
你没有设置打印参数吧,printdocument对象里设置好后再打印
------解决方案--------------------
打印参数没搞好吧,要根据你设置的纸张尺寸的参数来打印!
------解决方案--------------------
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
是不是这里的尺寸不对