I'm using a Label to display progress to the user. This occurs many times and the partly user-defined text changes every time.
Problem: I should know if the drawn text is bigger than the Label's size.
I tried this approach:
using (Graphics g = lbl.CreateGraphics()) {
SizeF size = g.MeasureString(lbl.Text, lbl.Font);
// Change size of label if too small
}
but it is too slow and uses a lot resources when calling at every update.
So is there a way to find out when the drawn text is bigger than the Label?
EDIT:
As stated by Hans Passant, AutoSize will do it.
Sorry, I didn't say that other controls below the Label has to move then.
It seems that there is no other way to do it.
So I have to use the above solution:
using (Graphics g = lbl.CreateGraphics()) {
SizeF size = g.MeasureString(lbl.Text, lbl.Font);
// Change size of label if too small
}
Related
I'm trying to scale a given text, e.g "123\nV.1-4", to fit the screen in mono.
I tried increasing the font size till the screen is not bigger.
My main problem is that I cannot retrieve the size of the label with a new font quickly?
(I'm retrieving with label.GetSizeRequest(out width, out height);)
So any solutions on how to scale the font/text?
Thanks
When the label is resized as you change the font size, the SizeRequest and SizeAllocated events will fire. What you could do is set the font size, then in the SizeAllocated event for the label, set the font size again. You will clearly have to stop the loop at some point, I assume you would cease increasing the font size when your label dimensions outstrip those of the window or screen etc.
However, have you considering using Pango and a drawing area instead?
I'm trying to determine the size of the textframe that will be needed for a block of text. This is to then be exported for an InDesign script to create the page. All in a console application.
I've tried to create a WPF TextBlock and assign the Text and a Width, but the Height and ActualHeight is NaN.
How can I determine the size of a textframe that will be needed for some text? Is using a WPF / Winforms textblock the best solution (to try and take advantage of existing code), or is there some other, better workflow?
There are two classes in C# that are used to draw text. TextRenderer and Graphics.
TextRenderer uses GDI to render the text, whereas Graphics uses GDI+. The two use a slightly different method for laying out text.
You can make use of Graphics.MeasureString or TextRenderer.MeasureText
Example
using( Graphics g = Graphics.FromHwnd(IntPtr.Zero) )
{
SizeF size = g.MeasureString("some text", SystemFonts.DefaultFont);
}
For your case I would suggest using TextRenderer. Text wrapping example -
var size = TextRenderer.MeasureText(text, font, new Size(width, height), TextFormatFlags.WordBreak);
The third argument is size of the drawing rectangle. You can pass height as 0 if you don't know it.
Am trying to change a label font size depending on screen resolution. Have tried when the form is loading, shown and also in the form constructor, but on screen the font size is the same as design time.
Rectangle resolution = Screen.PrimaryScreen.Bounds;
if (resolution.Width == 1024 && resolution.Height == 768)
{
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f);
}
I've added a double click event to the label to check the font size, and it says it's 40 in a message box (MessageBox.Show(this.labelEnterRegistration.Font.ToString());), so why doesn't the form display reflect this?
I have tried the label Invalidate()but that didn't work either.
Have fixed it. As it was done before it was setting the fonts emSize, I did the following so it changes the pixel size:
FontStyle style = this.labelEnterRegistration.Font.Style;
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f, style, GraphicsUnit.Pixe
And now keeps the same font style as well!!
Thanks for the comments #HEPÄ°MÄ°ZYARBAYMEHMETALKANIZ, made me think about it some more.
Before I go into my question, let me explain my setup:
First: I have a PictureBox that holds a Bitmap which is generated at runtime. This Bitmap can be different widths but always the same height.
Second: PictureBoxes do not support scrolling, therefore, I have the PictureBox docked in a panel. Initially, I had used the panel's autoscroll feature, but abandoned that after I discovered through this article that PictureBoxes have a size limit. I also learned that it's better to instead have small PictureBoxes and only draw what needs to be seen instead of the whole image.
Third: So I added a HScrollBar, which is fine and dandy, but I can't seem to figure out the math behind how big to make the scroller. I tried setting the maximum of the scrollbar to the length of the bitmap, but as you can see the size of the scroller is much smaller in mine than the one Windows puts in if I use the autoscroll feature.
My question is, what is the math behind the scroller size and how do I emulate that in my custom scrollbar?
Let me know if my question is unclear and I will try my best to make it more understandable. And thanks in advance for your help!
I figured out what was the problem. Perhaps I should have tried a little longer. :)
The answer lies in the LargeChange property. I let the Maximum at the total width of the bitmap and then set the LargeChange to the width of what I wanted to show. (i.e. the width of the PictureBox)
The size of the "scroller" is determined by the ratio of the value of LargeChange to the value of Maximum. For example, if the width to show (LargeChange) is 100 and the total width (Maximum) is 300 then the "scroller" size will be 1/3 of the scrollbar length. (100/300).
I got same problem too and tried to figure it out, I have a panel which contain another panel inside it called panelChild, and the default scrollbar is small, I need lager scrollbar, so I use HScrollBar to do that (display over-top of default scrollbar), I post my solution here, may be it helpful to someone
public Form() {
InitializeComponent();
hScrollBar.Maximum = panelChild.Width;
hScrollBar.LargeChange = panel.Width; // panel which contain panelChild, and this hScrollBar will same as panel scrollbar
hScrollBar.Scroll += HScrollBar_Scroll;
}
private void HScrollBar_Scroll(object sender, ScrollEventArgs e)
{
int diference = e.OldValue - e.NewValue;
foreach (Control c in panel.Controls)
{
c.Location = new Point(c.Location.X + diference, c.Location.Y);
}
}
i got a problem with image scaling in C#.
I have a picture Box with given Size : e.g. width = 800px height = 600px
I am loading different images into that picture box, small ones ( 400x400) and big ones (800+ x 600+)
My images are getting resized if they do not fit into box. But they are always resized to MAX width and height of PictureBox. So the aspect ratio is destroyed.
Can anybody help to identify / fix the problem?
Classes:
Form1.cs
ImageHandling.cs (commented out)
ImageUtilities.cs
Examples:
Problem 1:
My Version
vs
Original Source
Problem 2:
My Version
vs
Original Source
How i want it:
Solution
this.PictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
Set that property to your PictureBox and the size of the image will increased or decreased to fit the PictureBox maintaining the size ratio.
For more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode.aspx
I handled this by resetting the SizeMode on the PictureBox's resize method.
This is, essentially, the same answer as above, but it's formatted much better.
private void ScaleImage()
{
if (pbInfo.Image == null)
return;
if (pbInfo.Image.Width > pbInfo.Width || pbInfo.Image.Height > pbInfo.Height)
pbInfo.SizeMode = PictureBoxSizeMode.Zoom;
else
pbInfo.SizeMode = PictureBoxSizeMode.Normal;
}