473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

C# Font Scaling in a fixed width???

Hi, I was wondering if anyone could help me. I’m trying to add text into a fixed width. Say label1.MaximumSize = new Size(0, 30);
label1.text=textbox1.text
When the label gets full I want the text to start squeezing together and not just cut off. I keep playing with string formats ECT. But just can’t seem to figure it out. Is there a way to have a label or drawstring perform this kind of function???
Oct 30 '08 #1
11 10431
tlhintoq
3,525 Expert 2GB
Hi, I was wondering if anyone could help me. I’m trying to add text into a fixed width. Say label1.MaximumSize = new Size(0, 30);
label1.text=textbox1.text
When the label gets full I want the text to start squeezing together and not just cut off. I keep playing with string formats ECT. But just can’t seem to figure it out. Is there a way to have a label or drawstring perform this kind of function???
Here's some code from one of my functions that puts a text tag onto a jpg graphic. It works a little different from your need, as I extend the label to space required by all the text at a fixed size. You want to reduce the font to match a fixed space.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Font myFont = new Font("Arial", LableSize); // LableSize is the font size                    
  3. Graphics LGraphic = Graphics.FromImage(myLable);//myLable is an Image obj                 
  4. PaintEventArgs e = new PaintEventArgs(LGraphic, FullSize);//Fullsize is the size of the total graphic so I know how much space I have to work with later on                 
  5. SizeF oSize1 = e.Graphics.MeasureString(szLable, myFont);
  6.  
I'm thinking that you should be able to reverse engineer a loop from this that takes your fixed space, and a starts with a font size of 1, and keeps increasing the size until your text no longer fits, then drop back down to the size that worked and use that to display your text. Maybe something involving a check like:
Expand|Select|Wrap|Line Numbers
  1. int FontSizeToUse = 1;
  2. for (int Loop = 1; Loop < 25; Loop++)
  3. {
  4.      if (oSize1.Width > myTextBox.Width) 
  5.      {
  6.           FontSizeToUse = Loop - 1;
  7.           break;
  8.      }
  9. }
  10. // Use the FontSizeToUse for your lable
  11.  
Oct 31 '08 #2
Hi, thanks for the reply. Im using somthing like that now.
Expand|Select|Wrap|Line Numbers
  1. int FontSizeCount = 0;
  2. private void textBox1_TextChanged(object sender, EventArgs e)
  3.         {
  4.  
  5.             label1.MaximumSize = new Size(0, 30);
  6.             if (label1.Width > 232)
  7.             {
  8.                 label1.Font = ChangeFontSize(label1.Font, label1.Font.Size - 2);
  9.                 label1.Location = new Point(28, FontSizeCount + 33);
  10.                 label1.Refresh();
  11.                 FontSizeCount++;
  12.             }
  13.             if ( label1.Width < 219 && FontSizeCount > 0)
  14.             {
  15.                label1.Font = ChangeFontSize(label1.Font, label1.Font.Size + 2);
  16.                label1.Location = new Point(28, 33 - FontSizeCount);
  17.                label1.Refresh();
  18.                FontSizeCount--;
  19.             }
  20.  
  21.             label1.Text = textbox1.Text;
  22.  
  23.  
  24.         }
  25.  
  26.         static public Font ChangeFontSize(Font font, float fontSize)
  27.         {
  28.             if (font != null)
  29.             {
  30.                 float currentSize = font.Size;
  31.                 if (currentSize != fontSize)
  32.                 {
  33.                     font = new Font(font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
  34.                 }
  35.             }
  36.             return font;
  37.         }
  38.  
And it is working it decreasing and increasing the size of the fonts, but I need it to not make the fonts smaller, but to squeeze them together. I found something like what I need it to do, if you got http://www.yugiohcardmaker.net and add a text to the name it writes on the card, after it reaches its max point it starts to squeeze the text together. I’m not sure how I can do it, or if I need to think of another way...
Oct 31 '08 #3
tlhintoq
3,525 Expert 2GB
Hi, thanks for the reply. Im using somthing like that now.
Expand|Select|Wrap|Line Numbers
  1. int FontSizeCount = 0;
  2. private void textBox1_TextChanged(object sender, EventArgs e)
  3.         {
  4.  
  5.             label1.MaximumSize = new Size(0, 30);
  6.             if (label1.Width > 232)
  7.             {
  8.                 label1.Font = ChangeFontSize(label1.Font, label1.Font.Size - 2);
  9.                 label1.Location = new Point(28, FontSizeCount + 33);
  10.                 label1.Refresh();
  11.                 FontSizeCount++;
  12.             }
  13.             if ( label1.Width < 219 && FontSizeCount > 0)
  14.             {
  15.                label1.Font = ChangeFontSize(label1.Font, label1.Font.Size + 2);
  16.                label1.Location = new Point(28, 33 - FontSizeCount);
  17.                label1.Refresh();
  18.                FontSizeCount--;
  19.             }
  20.  
  21.             label1.Text = textbox1.Text;
  22.  
  23.  
  24.         }
  25.  
  26.         static public Font ChangeFontSize(Font font, float fontSize)
  27.         {
  28.             if (font != null)
  29.             {
  30.                 float currentSize = font.Size;
  31.                 if (currentSize != fontSize)
  32.                 {
  33.                     font = new Font(font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
  34.                 }
  35.             }
  36.             return font;
  37.         }
  38.  
And it is working it decreasing and increasing the size of the fonts, but I need it to not make the fonts smaller, but to squeeze them together. I found something like what I need it to do, if you got http://www.yugiohcardmaker.net and add a text to the name it writes on the card, after it reaches its max point it starts to squeeze the text together. I’m not sure how I can do it, or if I need to think of another way...
I don't think there is a way of affecting spacing or leading of a label. The control simply doens't have that much power.
Had you considered making a graphic of your text and placing that on the form? Then you can easily change the width of the graphic to space you need, resulting in skinnier letters.
Oct 31 '08 #4
Thanx tlhintoq, that would be fine, im still not sure how to do it.. do you mean somthing like.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                         bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  4.                         using (Graphics g = Graphics.FromImage(bm))
  5. {
  6.  g.DrawString(textBox1.Text, new Font(CardDescFontName, CardDescFontSize), Brushes.Black, new RectangleF(new PointF(30, 380), new SizeF(288, 90)));
  7.  
  8. }
  9.  
do you have an example of how I can do it??? thanx again
Oct 31 '08 #5
Still No Luck... I have looked all over Google, just cannot seem to get it. Can anyone please show me an example of how to draw text and have the letters squeezed together instead of cutting off, if they won’t fit in a fixed width area???
Nov 1 '08 #6
tlhintoq
3,525 Expert 2GB
Thanx tlhintoq, that would be fine, im still not sure how to do it.. do you mean somthing like.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                         bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  4.                         using (Graphics g = Graphics.FromImage(bm))
  5. {
  6.  g.DrawString(textBox1.Text, new Font(CardDescFontName, CardDescFontSize), Brushes.Black, new RectangleF(new PointF(30, 380), new SizeF(288, 90)));
  7.  
  8. }
  9.  
do you have an example of how I can do it??? thanx again
You're definately on the the right track. Use the .DrawString to draw your text on to a graphic. From their scale the graphic to match your fixed-size space.

I was recently reminded of user controls from another post. This might be a good use for such a thing. A custom user control that takes a text string and displays it as a graphic. Maybe a couple properties to set for which font to use and so forth. That way once you work out all the behavior you can easily drop it on other forms.
Nov 1 '08 #7
Thank you, i will try playing around with that...
Nov 1 '08 #8
Hi again, can someone look at this and tell me what I’m doing wrong. Maybe I’m just brain fried from trying to figure this out for 4 days. I’m new to c# and learning as I go. I can now get the image to stretch but I can’t get it to do the opposite and squeeze the text together...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Bitmap bm;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.                 bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  25.                 using (Graphics g = Graphics.FromImage(bm))
  26.                 {
  27.                     pictureBox1.Refresh();
  28.                     g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  29.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  30.  
  31.                     g.ScaleTransform(2f,1f);
  32.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  33.                 }
  34.  
  35.  
  36.                 using (Graphics h = pictureBox1.CreateGraphics())
  37.                 {
  38.                     h.DrawImageUnscaled(bm, 0, 0);
  39.                 }
  40.         }
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.         }
  44.     }
  45. }
  46.  
Nov 2 '08 #9
tlhintoq
3,525 Expert 2GB
Hi again, can someone look at this and tell me what I’m doing wrong. Maybe I’m just brain fried from trying to figure this out for 4 days. I’m new to c# and learning as I go. I can now get the image to stretch but I can’t get it to do the opposite and squeeze the text together...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Bitmap bm;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.                 bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  25.                 using (Graphics g = Graphics.FromImage(bm))
  26.                 {
  27.                     pictureBox1.Refresh();
  28.                     g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  29.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  30.  
  31.                     g.ScaleTransform(2f,1f);
  32.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  33.                 }
  34.  
  35.  
  36.                 using (Graphics h = pictureBox1.CreateGraphics())
  37.                 {
  38.                     h.DrawImageUnscaled(bm, 0, 0);
  39.                 }
  40.         }
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.         }
  44.     }
  45. }
  46.  
I think you can just set the SizeMode property of the PictureBox at design time to either StretchImage or AutoSize (whichever gives you the results you like the look of), then change line 36. Notice you are specifically telling it to draw unscaled.
Instead just set the image property of the PictureBox to your newly created image.
Expand|Select|Wrap|Line Numbers
  1. PictureBox1.Image = MyNewlyCreatedImageObject;
Nov 2 '08 #10
Hi, again.. first let me say thanx to tlhintoq for the help I have got so far. well I can now get the image to stretch but I can’t get it to do the opposite and squeeze the text together... anyone have any ideas or am I doing wrong.




Expand|Select|Wrap|Line Numbers
  1. Bitmap bm;
  2.  
  3. private void button1_Click(object sender, EventArgs e)
  4. {
  5. bm = new Bitmap(pictureBox1.Width , pictureBox1.Height);
  6. using (Graphics g = Graphics.FromImage(bm))
  7. pictureBox1.Refresh();
  8. g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  9. g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 18), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  10. g.ScaleTransform(3f,1f);
  11. g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  12. }
  13.  
  14. using (Graphics h = pictureBox1.CreateGraphics())
  15. {
  16. h.DrawImage(bm,0,0);
  17. }
  18. }
  19.  
  20.  
Nov 4 '08 #11
OK.. I Got It.. Some one over at msdn was able to figure it out.. Im going to Post it here incase anyone here needs somthing like this..

Expand|Select|Wrap|Line Numbers
  1. bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);   
  2.             using (Graphics g = Graphics.FromImage(bm))   
  3.             {   
  4.                 pictureBox1.Refresh();   
  5.                 SizeF sz = g.MeasureString(textBox1.Text, new Font("Stone Serif ITC TT", 18));   
  6.                 float sf = (pictureBox1.Width / sz.Width < pictureBox1.Height / sz.Height) ? pictureBox1.Width / sz.Width : pictureBox1.Height / sz.Height;   
  7.                 g.ScaleTransform(sf,sf);   
  8.                 g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 18), Brushes.Black, new PointF(0, 0), new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.NoClip));   
  9.             }   
  10.  
  11.             using (Graphics h = pictureBox1.CreateGraphics())   
  12.             {   
  13.                 h.DrawImage(bm, 0, 0);   
  14.             }  
  15.  
  16.  
Thanks for all the help..
Nov 5 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Csaba2000 | last post by:
How do I detect when the font size has been changed (especially by user action: either Ctrl+Scroll wheel or View/Text Size)? This is just for use on IE 5.5+, but it would be great if there was a...
4
by: Michel Joly de Lotbiniere | last post by:
I hope this is the correct newsgroup for this subject. The other day I came across a site www.safesquid.com that specified font-family:Terminal font-size:9pt in the inline css for command-line...
27
by: skeeterbug | last post by:
please see http://www.geocities.com/operationsengineer1/test.htm for an idea of how i want my logo header div to be layed out. when i went to resize "Test" to 2em (from 1em), this is what...
4
by: Eric Eggermann | last post by:
Hello all, I'm building an app that lays out and prints flash cards. Now the card might be any size, but when the user is working on the card, I draw the preview isotropically, as large as...
9
by: Adam | last post by:
Can someone please help!! I am trying to figure out what a font is? Assume I am working with a fixed font say Courier 10 point font Question 1: What does this mean 10 point font Question 2:...
4
by: Paul Gorodyansky | last post by:
Hi, I've read most of the discussions about "calculation of string width in pixels or points" - they mostly talk about MeasureString (and that it's not exact width) or more presize but...
7
by: carterweb | last post by:
This is how I do it now. 1. Determine the dimensions of the rectangle. 2. Set a my font size to a fixed maximum size. 3. Apply the font my string and measure the string using the graphics...
1
by: linksterman | last post by:
hey, I was experimenting with pyglet, and was trying out their opengl, but cant figure out how to scale images... #!/usr/bin/python # $Id:$ '''Demonstrates one way of fixing the display...
2
by: sam | last post by:
can someone show how to change print font width & heigh scaling? I am writing a code for barcode printing on smaller label. I do not want change font size, because this may cause the bar code...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.