Creating graphics object
-
Is it possible to create a graphics object in the constructor of a normal calss in order to measure a string? Not using a form or control?
Graphics g = ?
m_nodeSize = g.MeasureString(m_name, nodeFont);Thanx George
Graphics g = Graphics.FromImage(new Bitmap(80, 80));
Give that a shot. -
Is it possible to create a graphics object in the constructor of a normal calss in order to measure a string? Not using a form or control?
Graphics g = ?
m_nodeSize = g.MeasureString(m_name, nodeFont);Thanx George
-
Graphics g = Graphics.FromImage(new Bitmap(80, 80));
Give that a shot.Thank you Kevinnicol That worked great, i was looking for something like that.
public static SizeF MeasureString(string s, Font font)
{
SizeF result;
using (var image = new Bitmap(0, 0))
{
using (var g = Graphics.FromImage(image))
{
result = g.MeasureString(s, font);
}
}return result; }
Did see this example but preffer the below:
// Generate nodeSize by measuring the name of the node
Graphics g = Graphics.FromImage(new Bitmap(80, 80));
m_nodeSize = g.MeasureString(m_name, nodeFont);Thanx George
-
Thank you stancrm I saw this however couldnt find a solution as:
Graphics g;
// Sets g to a graphics object representing the drawing surface of the
// control or form g is a member of.
g = this.CreateGraphics();Only works in a control or on a form. Not in a normal C# class Thanx George
-
Thank you Kevinnicol That worked great, i was looking for something like that.
public static SizeF MeasureString(string s, Font font)
{
SizeF result;
using (var image = new Bitmap(0, 0))
{
using (var g = Graphics.FromImage(image))
{
result = g.MeasureString(s, font);
}
}return result; }
Did see this example but preffer the below:
// Generate nodeSize by measuring the name of the node
Graphics g = Graphics.FromImage(new Bitmap(80, 80));
m_nodeSize = g.MeasureString(m_name, nodeFont);Thanx George
gwithey wrote:
Graphics g = Graphics.FromImage(new Bitmap(80, 80)); m_nodeSize = g.MeasureString(m_name, nodeFont);
Sorry, did you mean to say that you prefer this version? If so, please don't - use the version above it instead as that disposes of managed resources instead.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Thank you stancrm I saw this however couldnt find a solution as:
Graphics g;
// Sets g to a graphics object representing the drawing surface of the
// control or form g is a member of.
g = this.CreateGraphics();Only works in a control or on a form. Not in a normal C# class Thanx George