3 Replies - 5425 Views - Last Post: 02 February 2010 - 04:06 AM Rate Topic: -----

#1 ath3i   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-October 09

GDI+ Resizable text in the middle of rectangle

Posted 01 February 2010 - 02:26 AM

Hello, I drew text inside rectangle. Text is in the middle of it.
How to make this text resizable, for example when I resize form rectangle grows, and text also grows but it is still in the middle of rectangle?

This is my code, it draws rectangle, text in the middle of it, when I resize rectangle grows, text remains in the middle, but it doesn't resize.

	   private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			//SCALE
			GraphicsPath myPath = new GraphicsPath();
			Pen pn1 = new Pen(Color.Green, 10);
			Rectangle rect1 = new Rectangle(5, 5, pictureBox1.Width-10, pictureBox1.Height-10);
			g.DrawRectangle(pn1, rect1);

			StringFormat strFmt3 = new StringFormat();
			strFmt3.Alignment = StringAlignment.Center;
			strFmt3.LineAlignment = StringAlignment.Center;
			myPath.AddString("Sample Text", new FontFamily("Tahoma"), (int)FontStyle.Bold, 86, rect1, strFmt3);
			
			HatchBrush hb1 = new HatchBrush(HatchStyle.Sphere, Color.Red, Color.Blue);
			Pen pn = new Pen(Color.Black, 5);
			g.DrawPath(pn, myPath);
			g.FillPath(hb1, myPath);
		}

For scale I've been trying different methods, but any of them doesn't seem to work properly ;/
 //1
float rozmInch = Math.Min(pictureBox1.Width / g.DpiX, pictureBox1.Height / g.DpiX);
g.ScaleTransform(rozmInch * g.DpiX / 500, rozmInch * g.DpiY / 500, MatrixOrder.Prepend);
//2
g.PageUnit = GraphicsUnit.Pixel;
SizeF sizef = g.VisibleClipBounds.Size;
g.PageScale = Math.Min(sizef.Width / g.DpiX / 5,  sizef.Height / g.DpiY / 5);
//3
float ratio = Math.Min(pictureBox1.Width / 608f, pictureBox1.Height / 351f);
Matrix mx = new Matrix(ratio, 0, 0, ratio, 0, 0);
g.Transform = mx;

Here is the code for this example if somebody wants http://www.sendspace.com/file/32pvsl

Is This A Good Question/Topic? 0
  • +

Replies To: GDI+ Resizable text in the middle of rectangle

#2 Ferencn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: GDI+ Resizable text in the middle of rectangle

Posted 01 February 2010 - 03:15 AM

You could try to change the fontsize of the text. I see that it is hardcoded to 86.
Perhaps you could calculate a fontsize depending on the the dimension of the box.
If you box's 'normal' size is 200 pixels wide, you could replace 86 with:

86*boxwidth/200. Then the text would shrink when the box gets narrower, and grow when the box gets wider.

This approach will keep the aspect of the font intact.
Of course you could also make the height of the box determine the fontsize.
Was This Post Helpful? 1
  • +
  • -

#3 ath3i   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-October 09

Re: GDI+ Resizable text in the middle of rectangle

Posted 01 February 2010 - 03:38 AM

Thanks very much for suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 Endkill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 10

Re: GDI+ Resizable text in the middle of rectangle

Posted 02 February 2010 - 04:06 AM

Think center of display or form.
0 1 2 3 4 5 6 7 8 9 0
1
2
3 x
4
5 c
6
7 m
8
9
0
In this grid you need to find the location of x.
the center c = form.width/2, form.height/2;
between x and m is your object so we need half it's width.
xCenter = m.width/2, m.height/2;
then subtract x from c.....
total = c.width - x.width, c.height - x.height

Also fixed the font size to 1/10th the height.

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int width = ClientRectangle.Width;
int height = ClientRectangle.Height;
//get the size of the string
SizeF sz = gr.MeasureString("Sample text.",
new Font(new FontFamily("Tahoma"),ClientRectangle.Height/10));
//Set the top-left point of the text
point pt = new Point((width/2) - (sz.Width/2),
(height/2) - (sz.Height/2))
GraphicsPath myPath = new GraphicsPath();
Pen pn1 = new Pen(Color.Green, 10);
//Fix Rect for whole Form.
Rectangle rect1 = new Rectangle(pt.X, pt.Y, sz.Width, sz.Height);
g.DrawRectangle(pn1, rect1);
StringFormat strFmt3 = new StringFormat();
strFmt3.Alignment = StringAlignment.Center;
strFmt3.LineAlignment = StringAlignment.Center;
myPath.AddString("Sample Text", new FontFamily("Tahoma"),
(int)FontStyle.Bold, ClientRectangle.Height/10, rect1, strFmt3);
HatchBrush hb1 = new HatchBrush(HatchStyle.Sphere, Color.Red,
Color.Blue);
Pen pn = new Pen(Color.Black, 5);
g.DrawPath(pn, myPath);
g.FillPath(hb1, myPath);
}

n/m the grid.... it killed my formatting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1