C# 印刷画框

C# 印刷画框,c#,winforms,C#,Winforms,当我尝试打印PictureBox时,会抛出ArgumentException(参数无效) 这是打印功能: void pdGroupBox_PrintPage(object sender, PrintPageEventArgs e) { foreach(Control control in _GbFrm.Controls) DrawControls(control, e.Graphics); } private void DrawControls(Control cont

当我尝试打印
PictureBox
时,会抛出
ArgumentException
(参数无效)

这是打印功能:

void pdGroupBox_PrintPage(object sender, PrintPageEventArgs e)
{
    foreach(Control control in _GbFrm.Controls)
        DrawControls(control, e.Graphics);
}

private void DrawControls(Control control,Graphics g)
{
    var font = new Font("Arial", 10);
    var brush = new SolidBrush(Color.Black);
    var drawFormat = new StringFormat
    {
        FormatFlags = StringFormatFlags.DirectionRightToLeft
    };

    if (control is Label)
    {
        if ((string)control.Tag == "1") //Treated with 1 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 160, control.Location.Y, control.Size.Width + 10, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "2") //Treated with 2 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 70, control.Location.Y, control.Size.Width + 10, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "3") //Treated with 3 columns of fields.
        {
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width + 10, control.Size.Height));
        }
    }
    else if (control is TextBox || control is ComboBox)
    {
        if ((string)control.Tag == "1") //Treated with 1 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X - 120, control.Location.Y, control.Size.Width - 40, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 120, control.Location.Y, control.Size.Width - 40, control.Size.Height), drawFormat);
        }
        if ((string)control.Tag == "2") //Treated with 2 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X - 30, control.Location.Y, control.Size.Width - 40, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X - 30, control.Location.Y, control.Size.Width - 40, control.Size.Height),
                drawFormat);
        }
        if ((string)control.Tag == "3") //Treated with 3 columns of fields.
        {
            g.DrawRectangle(
                new Pen(Color.Black, 1),
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height));
            g.DrawString(
                control.Text, font, brush,
                new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height), drawFormat);
        }
    }
    else if (control is PictureBox && control.Visible)
    {
        var img = ((PictureBox)control).Image;
        var bitmap = new Bitmap(img,new Size(img.Width,img.Height));
        g.DrawImage(
            bitmap,
            new Rectangle(control.Location.X, control.Location.Y, control.Size.Width, control.Size.Height));
    }
    else if (control is DateTimePicker)
    {
        g.DrawString(
            control.Text, font, brush,
            new Rectangle(control.Location.X, control.Location.Y, control.Size.Width + 10, control.Size.Height));
    }
}

所有打印问题都可以解决,但有时打印
图片盒会引发
异常
,因此如何解决此问题?

尝试以下方法:稍微简单一点:

在按钮单击事件中添加以下代码:

PrintDocument printDocument1 = new PrintDocument();
printDocument1.PrintPage +=new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
然后,printDocument1\u PrintPage事件:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(pic.Image, 0, 0);
}
其中pic是图片框


这将在picturebox中打印图像。

您可以发布异常吗?你说的打印是指打印机?或在屏幕上绘图?打印到打印机。但是这个代码中有一个例外:Bitmap Bitmap=newbitmap(img,newsize(img.Width,img.Height));因为包含(参数无效)的每个(img.Width、img.Height)异常消息都是“参数无效”。重复的:位图类是不允许忽略Dispose()方法的一个类。您的程序正在耗尽非托管内存。使用using语句确保位图在绘制后被释放。否则,创建新位图没有什么意义,只需按原样使用图像即可。使用获取矩形的DrawImage()重载。