预览时Windows窗体无法以正确的格式显示

时间:2012-05-19 09:29:21

标签: c# graphics printing

我想在C#中以Windows窗体的形式打印部分区域中包含的数据。这是我的DrawAll()方法,单击预览按钮时调用。我想只打印该部分中没有背景图像的数据。但它没有给出正确的格式, 这是正确的格式

enter image description here

这就是我得到的

enter image description here

这是DrawAll方法

`private void DrawAll(Graphics g)            {

       // RectangleF srcRect = new RectangleF(0, 0, this.pictureBox1.Width,       this.pictureBox1.Height);
        Rectangle srcRect=new Rectangle(0,0,this.pictureBox1.Width,this.pictureBox1.Height);
        int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
        int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
        Rectangle destRect = new Rectangle(0, 0, nWidth, nHeight /3);
       // Rectangle drawRect = (Rectangle)destRect.;
       // g.DrawImage(this.pictureBox1.BackgroundImage, destRect, srcRect,   GraphicsUnit.Pixel);
         Pen aPen = new Pen(Brushes.Black, 1);
         g.DrawRectangle(aPen,destRect);

        float scalex = destRect.Width / srcRect.Width;
        float scaley = destRect.Height / srcRect.Height;

       // Pen aPen = new Pen(Brushes.Black, 1);
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (Controls[i].GetType() == this.paytext.GetType())
            {
                TextBox theText = (TextBox)Controls[i];
                g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());
            }
            if (Controls[i].GetType() == this.label4.GetType())
            {
                Label theTextlbl = (Label)Controls[i];
                g.DrawString(theTextlbl.Text, theTextlbl.Font, Brushes.Black, theTextlbl.Bounds.Left * scalex, theTextlbl.Bounds.Top * scaley, new StringFormat());
            }
            if (Controls[i].GetType() == this.dateTimePicker1.GetType())
            {
                DateTimePicker theDate = (DateTimePicker)Controls[i];
                g.DrawString(theDate.Text, theDate.Font, Brushes.Black, theDate.Bounds.Left * scalex, theDate.Bounds.Top * scaley, new StringFormat());
            }
        }
    }`

0 个答案:

没有答案