Exception when CreateGraphics()

  • Thread starter Thread starter Flair
  • Start date Start date
F

Flair

I add an pictureBox control in Form1, then in Form1(), after
InitializeComponent(); this line of code:
g = this.pictureBox.CreateGraphics();
//g is a private member declared as: private Graphics g;
raise an exception says:
"An unhandled exception of type 'System.NotSupportedException' occurred in
System.Windows.Forms.dll
Additional information: NotSupportedException"

Can anybody help me what's going wrong? TIA
 
Thanks.
And what's the difference between these two ways below besides that I may
need to derive a new class when adopting the second?
1. pictureBox.paint += new PaintEventHandler(pictureBox_Paint);
2. override the OnPaint(PaintEventArgs e) method.

Sergey Bogdanov said:
Here is the quote from Smart Client FAQ [1]:
"Only the Control and Form classes support Control.CreateGraphics()."

if you want to get Graphics for PictureBox you should use Paint event
handler:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Black) , 0, 0, 200, 200);
}


[1]
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#2.5

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I add an pictureBox control in Form1, then in Form1(), after
InitializeComponent(); this line of code:
g = this.pictureBox.CreateGraphics();
//g is a private member declared as: private Graphics g;
raise an exception says:
"An unhandled exception of type 'System.NotSupportedException' occurred
in System.Windows.Forms.dll
Additional information: NotSupportedException"

Can anybody help me what's going wrong? TIA
 
As you know both methods are correct. But the first method is more
simple but and not so reusable as the second. If you are going to derive
your class from PictureBox and override OnPaint method then you should
know that VS designer will stop displaying your NewPictureBox. To avoid
this problem you should create designer support for this custom control
(as an example see my PictureBoxEx that support transparency -
http://www.sergeybogdanov.com/Samples/PictureBoxEx.zip) and also about
this theme take a look at Alex Yakhnin's article
http://www.intelliprog.com/articles/index.html

Hope this help,
Sergey Bogdanov
http://www.sergeybogdanov.com
Thanks.
And what's the difference between these two ways below besides that I may
need to derive a new class when adopting the second?
1. pictureBox.paint += new PaintEventHandler(pictureBox_Paint);
2. override the OnPaint(PaintEventArgs e) method.

Here is the quote from Smart Client FAQ [1]:
"Only the Control and Form classes support Control.CreateGraphics()."

if you want to get Graphics for PictureBox you should use Paint event
handler:

private void pictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Black) , 0, 0, 200, 200);
}


[1]
http://msdn.microsoft.com/smartclient/understanding/netcf/FAQ/default.aspx#2.5

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
I add an pictureBox control in Form1, then in Form1(), after
InitializeComponent(); this line of code:
g = this.pictureBox.CreateGraphics();
//g is a private member declared as: private Graphics g;
raise an exception says:
"An unhandled exception of type 'System.NotSupportedException' occurred
in System.Windows.Forms.dll
Additional information: NotSupportedException"

Can anybody help me what's going wrong? TIA
 
Back
Top