0

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

在此处输入图像描述

这就是我得到的

在此处输入图像描述

这是 DrawAll 方法

`私人无效DrawAll(图形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());
            }
        }
    }`
4

0 回答 0