2012-12-26 124 views
2

當我嘗試打印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)); 
    } 
} 

所有的印刷問題開展工作,但有些時候打印PictureBox提高該Exception,所以我怎麼能解決這個問題?

+1

你能發佈異常嗎?打印你的意思是打印機?或在屏幕上繪圖? – elyashiv

+0

打印到打印機。但例外來自此代碼: 位圖位圖=新位圖(img,新尺寸(img.Width,img.Height)); 因爲每個(img.Width,img.Height)包含(參數無效) –

+0

異常消息是「參數無效」。 –

回答

4

試試這個:有些簡單:

添加以下代碼在按鈕單擊事件:

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

然後printDocument1_PrintPage事件:

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

其中PIC是圖片框。 。

這將在圖片框中打印圖像。