2

When I try to print a PictureBox, an ArgumentException is thrown (parameter is not valid).

This is the printing functions:

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));
    }
}

All the printing issues work, but some times printing a PictureBox raises that Exception, so how can I solve this problem?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
alaa joma
  • 41
  • 2
  • 2
  • 3
  • 1
    can you post the exception? by print you mean to the printer? or drawing on the screen? – elyashiv Dec 26 '12 at 11:17
  • print to printer. but the exception come from this code: Bitmap bitmap = new Bitmap(img,new Size(img.Width,img.Height)); because each (img.Width,img.Height) containing (parameter is not valid) – alaa joma Dec 26 '12 at 11:22
  • the exception message is "Parameter is not valid." – alaa joma Dec 26 '12 at 11:26
  • Duplicate of: [parameter is not valid creating new bitmap](http://stackoverflow.com/questions/6333681/c-sharp-parameter-is-not-valid-creating-new-bitmap) – Blachshma Dec 26 '12 at 11:36
  • 1
    The Bitmap class is the one class that doesn't allow you to ignore the Dispose() method. Your program is running out of unmanaged memory. Use the *using* statement to ensure the bitmap is disposed after drawing it. There is otherwise very little point to creating a new bitmap, just use the Image as-is. Use the DrawImage() overload that takes a Rectangle. – Hans Passant Dec 26 '12 at 15:06

1 Answers1

10

Try this: somewhat simple:

Add the following code in the button click event:

PrintDocument printDocument1 = new PrintDocument();
printDocument1.PrintPage +=new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();

Then the printDocument1_PrintPage event :

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(pic.Image, 0, 0);
}

where pic is the picture box..

This prints the image in the picturebox.

Uthistran Selvaraj
  • 1,371
  • 1
  • 12
  • 31