views:

18

answers:

1

I'm trying to create a taskbar Icon in C# that would display a number representing the current processor time of an application. I can figure out the proper size of the icon by simply referencing the default taskbar icons dimesions, however, I run into a problem. Different Icon sizes work well with different font sizes of text. I need to figure out how to select a font size that would be as legible and clear as possible given the size of the Icon to be created.

In this case, font size 18 works well as the bitmap resolution of the Icon I'm creating is 32x32 (as my monitor has a 1680 x 1050 resolution). However, suppose the Icon to be made has a resolution of 16x16 (or something smaller than 32 x 32) - then a font size of 18 would be too large for the icon itself. Is there a method call I can make, or something along those lines that would tell windows to figure out the best font-size to use depending on resolution of the icon it is to be displayed on? I'm sorry if this is a confusing post, I would be happy to clarify anything.

 using (var image = new Bitmap(Icon.Width, Icon.Height)) 
        using (var g = Graphics.FromImage(image))
        {
            g.DrawString(cpuReading.ToString(), new Font("Times New Roman", 18), new SolidBrush(Color.LightGreen), new PointF());
            this.tskBarIcon.Icon = Icon.FromHandle(image.GetHicon());
        }
A: 

There is no direct method to do what you want.

But what you can do is use Graphics.MeasureString to measure the size of a rendered string. Start measuring with a large font size, and then try progressively smaller sizes until you get to one that will fit your icon dimensions.

Andrew Russell