Where is PaddingF? And Other Padding Oddities

1 Comment »

There is No PaddingF

There is Point and PointF, Size and SizeF, Rectangle and RectangleF, Padding and… wait, there is no PaddingF!

System.Drawing vs. System.Windows.Forms

The 2-D drawing functions in the System.Drawing namespace accept both integer and floating point measurements.  That’s why the main 2-D drawing structures have both int and float versions such as Point and PointF, respectively.

However, the Padding structure is defined separately in the System.Windows.Forms namespace where most 2-D measurements are in integers.  Hence, there is an integer Padding structure but no floating point PaddingF.

Read the rest of this entry »

Graphics.MeasureString Exception: Parameter is Invalid

1 Comment »

My C# program threw an exception from the following method:

SizeF size = Graphics.MeasureString( text, font );

Unfortunately, the exception’s message was rather obscure: “Parameter is invalid.” 

After an investigation, I discovered the error occurred because the font had been previously disposed

Unfortunately, the Font class does not have the typical IsDisposed property, so there was no way to directly query the font’s disposed state.  But I searched in my code and found where I was calling the font’s Dispose method, and then incorrectly trying to use the font again in my call to the MeasureString method.

Extract Application’s Own Icon in C#


It’s easy to extract an application’s own icon in C#:

form.Icon = Icon.ExtractAssociatedIcon( Application.ExecutablePath );

The ExtractAssociatedIcon static method on the Icon class (in System.Drawing) extracts the associated icon from any file.  By supplying the Application.ExecutablePath (in System.Windows.Forms), you extract the application’s own icon.

Padding: Like a Rectangle, but Not


The .NET 2.0 upgrade included many minor improvements that are easily overlooked. One such improvement is Padding, a handy structure in the System.Windows.Forms namespace that you may find useful for representing offsets, margins or padding in the user interface.

Read the rest of this entry »

Change Font Style


Changing a font style is a bit easier than changing its size, as there is a Font constructor that accepts a font and style as arguments. For example, to bold a label’s font:

Read the rest of this entry »

Change Font Size


An inspection of the Font class will reveal that every public property is read-only. This means to change a font’s size, you need to create a new Font object with all the same properties of your current font but with the new size. Here is a handy method to do just that:

Read the rest of this entry »

Lighten and Darken Colors in .NET


There is a very handy .NET class called ControlPaint in the System.Windows.Forms namespace that enables you to draw your own controls and control elements using the standard Windows style and theme. Buried in this rich class are four methods that enable you to lighten and darken colors:

Read the rest of this entry »

C# GetPixel and SetPixel


It’s hard to believe the comprehensive .NET framework would omit such obvious functions as GetPixel and SetPixel from its Drawing library. Fortunately, we can access the GDI functions using Interop, as shown below. Notice the conversion required between the COLORREF integer used by the GDI methods and the Color structure used by our static .NET methods.

Read the rest of this entry »

Embedded Image Resources


If you use images in a .NET application, chances are you will find it more convenient to embed those images as resources in your project, rather than leaving them as separate files and trying to locate and load the images from disk when the application runs.

Read the rest of this entry »