CreateGraphics() throws NotSupportedException

  • Thread starter Thread starter Christian Schwarz
  • Start date Start date
C

Christian Schwarz

Hello,

I get a NotSupportedException when using the following code snippet in the
constructor of my ImageButton control:

System.Drawing.Graphics gfx = this.CreateGraphics();
SizeF textSizeF = gfx.MeasureString(this.Text, this.Font);
this.m_TextSize = new Size((int)Math.Floor(textSizeF.Width),
(int)Math.Floor(textSizeF.Height));
gfx.Dispose();

To be exact, the exception is thrown by the first line. Could anyone explain
why CreateGraphics() doesn't work here ?

Christian
 
Not sure what "this" is in the context of your code, but ti's likely not an
object that supports CreateGraphics. I think the only items that support
CreateGraphics are Control, Panel, Form and PictureBox.
 
Hello Christian,

The CreateGraphics() is supported only for the Control and Form classes.
Calling this method for any other classes will throw NotSupportedException.

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Christian Schwarz" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Subject: CreateGraphics() throws NotSupportedException
| Date: Wed, 17 Sep 2003 17:15:33 +0200
| Lines: 17
| Message-ID: <[email protected]>
| NNTP-Posting-Host: p50886498.dip0.t-ipconnect.de (80.136.100.152)
| X-Trace: news.uni-berlin.de 1063811687 28238486 80.136.100.152 (16
[57495])
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.freenet.de!fu-berlin.de!uni-berlin.de!p50886498.dip0.t-ipconne
ct.DE!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:33755
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Hello,
|
| I get a NotSupportedException when using the following code snippet in the
| constructor of my ImageButton control:
|
| System.Drawing.Graphics gfx = this.CreateGraphics();
| SizeF textSizeF = gfx.MeasureString(this.Text, this.Font);
| this.m_TextSize = new Size((int)Math.Floor(textSizeF.Width),
| (int)Math.Floor(textSizeF.Height));
| gfx.Dispose();
|
| To be exact, the exception is thrown by the first line. Could anyone
explain
| why CreateGraphics() doesn't work here ?
|
| Christian
|
|
|
 
Thank you for the answers.

My ImageButton's base class is System.Windows.Forms.Button, which you say
doesn't support CreateGraphics() method. What can I do, if I need the
CreateGraphics() method in order to measure a string's size ? Creating a
dummy control to use it's CreateGraphics() method ?

Christian
 
You can inherit your custom button from the Control class. Then you will be
able to call the CreateGraphics() method.

Hope this helps,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| From: "Christian Schwarz" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Subject: Re: CreateGraphics() throws NotSupportedException
| Date: Thu, 18 Sep 2003 08:25:52 +0200
| Lines: 10
| Message-ID: <[email protected]>
| References: <[email protected]>
| NNTP-Posting-Host: p50886498.dip0.t-ipconnect.de (80.136.100.152)
| X-Trace: news.uni-berlin.de 1063866203 28541299 80.136.100.152 (16
[57495])
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
8.phx.gbl!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!
fu-berlin.de!uni-berlin.de!p50886498.dip0.t-ipconnect.DE!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:33818
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Thank you for the answers.
|
| My ImageButton's base class is System.Windows.Forms.Button, which you say
| doesn't support CreateGraphics() method. What can I do, if I need the
| CreateGraphics() method in order to measure a string's size ? Creating a
| dummy control to use it's CreateGraphics() method ?
|
| Christian
|
|
|
 
Call CreateGraphics on Button's parent form:

Form frm = myButton.TopLevelControl as Form;
if ( frm != null )
{
Graphics g = frm.CreateGraphics();
g.MeasureString(myButton.Text, myButton.Font);
}
 
Back
Top