Forum - I have a small WinForms app that interacts over the USB with a microcontroller which digitizes an applied anaolg voltage and plots the points in a pictureBox control. The operational code and some GUI control code are in a DoLoop in a thread set up and called by the Run_btn_Click event handler. The code is C++ Windows::Forms being developed under Visual C++ Windows::Forms 2008.
The Run_btn_Click handler code looks like this:
1 2 3 4 5
|
private: System::Void Run_btn_Click(System::Object^ sender, System::EventArgs^ e) {
Thread^ thread = gcnew Thread(gcnew ThreadStart(this, &Form1::DoLoop));
thread->IsBackground = true;
thread->Start();
} // End of Run_btn_Click
| |
All app code compiles; it all runs fine except for the printDocument1_PrintPage handler (which is called by the printToolStripMenuItem_Click handler) ; this PrintPage handler throws an Unhandled Exception error when the Print button in the printDialog window (associated with the printToolStripMenuItem_Click event) is clicked.
The Details section of the error window reads (in part):
************** Exception Text **************
System.ArgumentNullException: Value cannot be null.
Parameter name: image
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)
at SimpleWinUSBDemo.Form1.printDocument1_PrintPage(Object sender, PrintPageEventArgs e) in c:\program files (x86)\microsoft visual studio 9.0\projects\loggeri2r4\form1.h:line 990
----------------------------<other lines>-------------------------------
at System.Drawing.Printing.PrintDocument.Print()
at SimpleWinUSBDemo.Form1.printToolStripMenuItem_Click(Object sender, EventArgs e) in c:\program files (x86)\microsoft visual studio 9.0\projects\loggeri2r4\form1.h:line 984
---------------------------<plus other lines>--------------------------
The printToolStripMenuItem_Click and printDocument1_PrintPage handlers with problem lines marked are shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
private: System::Void printToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
PrintDocument^ printDocument1 = gcnew PrintDocument;
printDocument1->PrintPage += gcnew PrintPageEventHandler(this, &SimpleWinUSBDemo::Form1::printDocument1_PrintPage);
if(printDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
printDocument1->Print(); // Line 984 Unhandled Exception
}
} // End of printToolStripMenuItem_Click
private: System::Void printDocument1_PrintPage(System::Object^ sender, System::Drawing::Printing::PrintPageEventArgs^ e)
{
e->Graphics->DrawImage(pictureBox1->Image,0,0); // Line 990 Unhandled Exception
} // End of _PrintPage
| |
The above handlers work fine in a single-thread version of this code.
Assuming the above means I am not extracting the pictureBox Image into the PrintPage routine, I've tried for over a week to adapt examples of invokes, delegates, etc. without success and I am simply not getting the idea and could really use a bit of help. Also, if my fundamental approach to extracting and printing that image is not correct I'd very much like to be corrected in that direction with what is the correct (or a better) way to do it. Thanks in advance for any assistance the Forum might care to offer me.
MickH